简体   繁体   English

如何使用 JUnit 正确测试主 class 中定义的方法?

[英]how to properly test the methods defined in the Main class by using JUnit?

I am new to Java so this could be a naive question.我是 Java 的新手,所以这可能是一个幼稚的问题。 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.我创建了一个Main class 如下所示,我想为getArraysgetAveragebubbleSortAscending方法编写一些测试,只是为了学习如何进行单元测试。

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: IntelliJ 自动为我生成了这些测试代码:

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... IntelliJ 告诉我出了点问题... 在此处输入图像描述

So looks like it's because the getArrays() inside the MainTest is not taking any input argument?所以看起来是因为MainTest中的getArrays()没有接受任何输入参数? Why the getArrays() in the MainTest is not working the same as the getArrays() defined in the Main ?为什么MainTest中的getArrays() getArrays()Main中定义的 getArrays() 工作方式不同?

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.与测试方法( MainTest.getTest() Main.getTest(int) )相反,测试方法( MainTest.getTest() )没有任何参数 - 因此出现错误消息,您无法将int传递给该方法。

You have to call the tested static method by specifying the class:您必须通过指定 class 来调用经过测试的 static 方法:

@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:现在您可以使用 static 导入并调用经过测试的 static 方法,而无需对 class 进行分类:

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

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

IntelliJ tip : IntelliJ 提示

Hold CTRL and click on the method name.按住 CTRL 并单击方法名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM