Home > Enterprise >  how to properly test the methods defined in the Main class by using JUnit?
how to properly test the methods defined in the Main class by using JUnit?

Time:12-29

I am new to Java so this could be a naive question. I created a Main class like below, and I would like to write some tests for the getArrays, getAverage, and bubbleSortAscending methods, just for the purpose of learning how to do unit tests.

public class Main {
        private static final Scanner scanner = new Scanner(System.in);
        public static void main(String [] args) {
            int[] myArrays = getArrays(5);
            for (int i=0; i<myArrays.length; i  ) {
                System.out.println("index = "   i   " value = "   myArrays[i]);
            }

            System.out.println("Average is "   getAverage(myArrays));

            int[] sortedArray = bubbleSortAscending(myArrays);
            System.out.println("Sorted: \r");
            for (int i=0; i<sortedArray.length; i  ) {
                System.out.println(sortedArray[i]);
            }
        }

        public static int[] getArrays(int number) {
            System.out.println("Enter "   number   " integer values.\r");
            int[] values = new int[number];

            for (int i=0; i<values.length; i  ){
                values[i] = scanner.nextInt();
            }
            return values;
        }

        public static double getAverage(int[] array) {
            double sum = 0;
            for (int i=0; i<array.length; i  ){
                sum  = array[i];
            }
            return sum / (double) array.length;
        }

        public static int[] bubbleSortAscending(int[] array) {
            for (int i=0; i<array.length-1; i  ){
                for (int j=0; j<array.length - i - 1; j  ){
                    int a = array[j];
                    int b = array[j 1];
                    if (a > b) {
                        int c = a;
                        a = b;
                        b = c;
                    }
                    array[j] = a;
                    array[j 1] = b;
                }
            }
            return array;
        }

    }

And IntelliJ automatically generated these test codes for me:

public class MainTest {

    @org.junit.Test
    public void getArrays() {
    }

    @org.junit.Test
    public void getAverage() {
    }

    @org.junit.Test
    public void bubbleSortAscending() {
    }

}

However, when I filled the first one with codes like this:

 @org.junit.Test
    public void getArrays() {
        int[] expectedArray = new int[]{1,2,3,4,5};
        int[] generatedArray = getArrays(5);
        assertArrayEquals(expectedArray, generatedArray);
    }

IntelliJ told me that something is wrong ... enter image description here

So looks like it's because the getArrays() inside the MainTest is not taking any input argument? Why the getArrays() in the MainTest is not working the same as the getArrays() defined in the Main?

CodePudding user response:

The issue is that you are recursively calling (by mistake) the test method itself, which happens to have the same method name as the tested method.

The test method (MainTest.getTest()), in contrary to the tested method (Main.getTest(int)) does not have any parameter - hence the error message, that you cannot pass the int to that method.

You have to call the tested static method by specifying the class:

@Test
public void getArrays() {
   ...
   int[] generatedArray = Main.getArrays(5); // Call getArrays(int) in Main class
   ...
}

or change the method name, you will probably have more than one test method anyways:

@Test
public void getArraysReturnsNull() { ... }
// OR
@Test
public void testGetArrays() { ... }
...

Now you can use static import and call tested static method without clasifying the class:

import static segovia.java.learn.Main.getArrays;

@Test
public void testGetArrays() {
   int[] generatedArray = getArrays(); // OK now
}

IntelliJ tip:

Hold CTRL and click on the method name.

  • Related