是否可以为 main 方法编写一个测试函数?

Is it possible to write a test function for the main method?

是否可以测试Java中的主要方法?

比如我有一个小程序

public class Number {
    public static int add(int zahl1, int zahl2) {
        return zahl1 + zahl2;
    }

    public static void main(String[] args) {

        int numberOne;
        int numberTwo;
        int total;
        int difference;
        int product;
        int quotient;
        int rest;

        Scanner input;
        input = new Scanner(System.in);
        input.useDelimiter(System.lineSeparator());

        System.out.println("First Number: ");
        numberOne = tastatur.nextInt();
        System.out.println("Second Number: ");
        NumberTwo = tastatur.nextInt();

        System.out.println("Thanks for the numbers");

        total = add(numberOne,numberTwo);

        System.out.println("The total of " + numberOne + " and " + numberTwo
                + " is " + total);
        
        difference = numberOne - numberTwo;
        product = numberOne * numberTwo;
        quotient = numberOne / numberTwo;
        rest = numberOne % numberTwo;
        
        System.out.println(differenz);
        System.out.println(product);
        System.out.println(quotient + "rest: " + rest );

    }
}

我的测试class:

public class RechenprogrammTest {
    @Test
    public void test() {
        int numberOne = 4;
        int numberTwo = 5;
        int total = Number.add(numberOne, numberTwo);
        assertEquals(9, total);
    }
}

是否只有一种方法我必须使用与 add 相同的方法对差值、乘积、商和 rest 进行计算,或者我可以像测试 add 方法一样测试 main 方法吗?

我什么都没试。我只是搜索了它,但没有收到任何好的答案。

main 方法与其他方法一样。你可以用 Number.main(new String[0]) 来调用它。在您当前的实现中,您会看到 System.in 的使用将阻止执行,您必须找到一种方法将其抽象掉。例如:

class Number {
    private InputStream inpuStream = System.in;
    
    // your main method
    
    public void setInputStream(InputStream inpuStream)
    {
        this.inpuStream = inpuStream;
    }
}

更好的解决方案是将逻辑提取到单独的方法中并对其进行测试。

您可以将代码移出 main 方法并将其放入自己的方法中。从这里您可以为您的新方法编写测试。对于测试,我想你会想要某种模拟数据注入而不是等待系统输入。

private static int[] calculateEverything(int numberOne, int numberTwo) {
    int total = numberOne + numberTwo;
    int diff = numberOne - numberTwo;
    int product = numberOne * numberTwo;
    int quotient = numberOne / numberTwo;
    int rest = numberOne % numberTwo;
    return new int[]{total, diff, product, quotient, rest};
}

@Test
public void testResultFor_1_2() {
    int[] results = calculateEverything(1,2);
    Assert.assertEquals(3, results[0]);
    Assert.assertEquals(-1, results[1]);
}

public static void main(String[] args) {
    Scanner input;
    input = new Scanner(System.in);
    input.useDelimiter(System.lineSeparator());

    System.out.println("First Number: ");
    int numberOne = input.nextInt();
    System.out.println("Second Number: ");
    int numberTwo = input.nextInt();
    System.out.println("Thanks for the numbers");

    int[] calculateResults = calculateEverything(numberOne, numberTwo);
    System.out.println("The total of " + numberOne + " and " + numberTwo
            + " is " + calculateResults[0]);
    
    System.out.println(calculateResults[1]);
    System.out.println(calculateResults[2]);
    System.out.println(calculateResults[3] + "rest: " + calculateResults[4]);
}

编辑:如果您希望所有内容都集中在一个方法中,您可以将代码调整为returns结果的方法,然后在测试中断言这些结果。