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

Is it possible to test the main method in Java?

For example, I have a little program.

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 );

    }
}

My Test class :

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

Is there only one way that I have to make for difference, product, quotient, and rest the same method as add or can I test the main method as I tested the add method?

I did not try anything. I had only searched for it but didn´t receive any good answers.



Solution 1:[1]

You can move your code out of the main method and put it in its own method. From here you can write tests for your new method. For testing, id imagine youd want some kind of mock data injection instead of awaiting system input.

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]);
}

Edit: If you want everything in one method you can adapt your code to a method that returns the results, then assert those results in the tests.

Solution 2:[2]

The main method is a method like any other. You can call it with Number.main(new String[0]). In your current implementation you will see, that the usage of System.in will block the execution, you have to find a way to abstract it away. For example:

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

A better solution would be to extract the logic to a separate method and test it instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Valerij Dobler