#Problems with method type or recognition

31 messages · Page 1 of 1 (latest)

graceful cradle
#

Hey, I'm quite new to Java and I'm struggling to figure out why my method isn't being recognised by JUnit.

The code

So the goal of the provided code is to print out an int array made up of random int to the console.

        String str = "";
        int length = randomSequence.length;
        for(int i =0; i < length; i++){
            str =x[i]+"";
            System.out.print(str);
        }
        return str;
        }```
```public static void printArrayToConsole(int[] x){
        String str = "";
        int length = randomSequence.length;
        for(int i =0; i < length; i++){
            str =x[i]+"";
            System.out.print(str);
        }
    }```
As you can see I've come up with 2 ways for this method.
The 1st returns a String, the 2nd is a void method.
Both work and print out an int array to the console, however JUnit doesn't care and gives me the same error.

## JUnit Errors

 >There was 1 failure:
    >1) runPrintArray(BubbleSortWithSideEffectsTest)
    >java.lang.AssertionError: could not find or execute method; printArrayToConsole(...);


I can´t provide the whole JUnit, so here´s a small portion of the code:

```assertTrue("error with printArrayToConsole. Content was not printed to console",printedOutA);

            System.setOut(System.out);
            System.setIn(System.in);
        }catch(Exception e){
            throw new AssertionError("could not find or execute method; " +
                            "printArrayToConsole(...);");
        }```

I would be very grateful if someone could explain me why none of the above methods works. Thank you in advance.
neat pythonBOT
#

This post has been reserved for your question.

Hey @graceful cradle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

graceful cradle
#

Fyi: This is what I´ve been asked for:

#

Create a method public static printArrayToConsole(int[] x) that can be used to
print out any int array to the shell.

#

But I don´t see how to create said method without it having a return type or being void

feral moss
#

Of course it needs to be void

#

As for why you have your error we can't guess. You chose not to show the code

#

Note that JUnit tests would normally not contain any try/catch. If you hadn't put that try/catch then you would have obtained a more helpful error message

graceful cradle
#
import static org.junit.Assert.*;
import java.io.*;

public class BubbleSortWithSideEffectsTest
{
    @Test
    public void runMainMethod(){
        String[] a = {""};
        BubbleSort.main(a);
    }

    @Test
    
    private static final ByteArrayOutputStream fromConsole = new ByteArrayOutputStream();
    private static PipedOutputStream outputToConsole;
    private static PipedInputStream inputOfConsole;
    private static String outputString;
    
    @Test
    public void runPrintArray(){
        try{
            outputToConsole = new PipedOutputStream();
            inputOfConsole  = new PipedInputStream(outputToConsole);
            System.setIn(inputOfConsole);
            System.setOut(new PrintStream(fromConsole));
            
            int[] a = {1,2,3,4,5,6};
            BubbleSort.printArrayToConsole(a);
            outputString = fromConsole.toString();
            boolean printedOutA = false;
            if (outputString.contains("1") && outputString.contains("2") &&
                outputString.contains("3") && outputString.contains("4") &&
                outputString.contains("5") && outputString.contains("6")){
                    printedOutA = true;
                }
            assertTrue("error with printArrayToConsole. Content was not printed to console",printedOutA);

            System.setOut(System.out);
            System.setIn(System.in);
        }catch(Exception e){
            throw new AssertionError("could not find or execute method; " +
                            "printArrayToConsole(...);");
        }
        
    }

    
}```
graceful cradle
graceful cradle
feral moss
#

That's because that catch is stupid. It displays idiocies. I imagine you didn't write the unit test yourself?

feral moss
#

I guess you'll have to add an e.printStackTrace(); before the throw, so that you get a useful error for the time being

#

Though I suspect your usage of a variable called randomSequence is wrong

graceful cradle
#

wait I can send you my code

feral moss
#

I thought you already did that

graceful cradle
#
    public static int[] randomSequence;
    public static void main(String[] Args){
        randomSequence = generateNewRandomArray(20);
        printArrayToConsole(randomSequence);
        
        //sortArray(randomSequence);
        
    }
    public static int[]generateNewRandomArray(int x){
        
        int[]randomArray = new int[x];
        
        for(int length =0; length <= x-1; length++){
        double dWert = Math.random()*1000+1;
        int iWert = (int)dWert;
        randomArray[length]= iWert;
        }
        return randomArray;
    }
    public static void printArrayToConsole(int[] x){
        String str = "";
        int length = randomSequence.length;
        for(int i =0; i < length; i++){
            str =x[i]+"";
            System.out.print(str);
        }
    }
}```
graceful cradle
feral moss
#

Anyway nobody told you you could use an external variable and check its length

#

So most likely you're wrong to do so

graceful cradle
#

Wdym?

feral moss
#

Your method printArrayToConsole(int[] x)
You were told it has to do stuff

#

These stuff did not include caring about an external variable called randomSequence

#

So, you doing that is most likely wrong

graceful cradle
#

Hmm, you could be right about this because the task only said: Create a method public static printArrayToConsole(int[] x) that can be used to
print out any int array to the shell.

#

Yes I just ran it through JUnit and that was it

#

😄

#

Thank you so much, you saved me hours worth of trying to work this out

neat pythonBOT