Search code examples
javajunitjunit4

Unable to print all test results in a testcase using JUnit


I have written a TestCase using a Runner class to print the results. But when I try to print the results, I am getting only the first result and not all failures/results of the TestCase.

My TestCase class is as below:

public class Fixtures_JavaTest extends TestCase{

    protected int value1, value2;
    
    
    public Fixtures_JavaTest(String str) {
        super(str);
    }
    
    @Override
    protected void setUp() throws Exception {

        value1 = 2;
        value2 = 5;
        
    }
    

    public void testAdd() {
        
        assertEquals(7, 8);
        assertEquals(4, 8);
        assertEquals(3, 8);
        assertEquals(2, 2);

    }
    
    
}

My runner class is as below:

public class Runner_Fixtures_JavaTest {
    public static void main(String[] args) {

        TestCase  test  =  new Fixtures_JavaTest("add") {
                
            public void runTest() {
                testAdd();
            }
        };
        
        
        TestResult result = test.run();
        
        
        Enumeration<TestFailure> failures = (Enumeration<TestFailure>) result.failures();
        
        while(failures.hasMoreElements()) {
            System.out.println(failures.nextElement());
            
        }
        

    }
}

I am getting the below result only

add(Runner_Fixtures_JavaTest$1): expected:<7> but was:<8>

Why am I not getting the output for below assertEqual methods?

assertEquals(4, 8);
assertEquals(3, 8);
assertEquals(2, 2);

Solution

  • JUnit tests are fail-fast tests. The first assertion that fails will fail the test, and it won't continue executing. If you want to test all the assertions, you could use a parameterized test. E.g.:

    @RunWith(Parameterized.class)
    public class Fixtures_JavaTest {
        private int value1;
        private int value2;
    
        public Fixtures_JavaTest(int value1, int value2) {
            this.value1 = value1;
            this.value2 = value2;
        }
    
        @Parameters
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][] {
                    {7, 8}, {4, 8}, {3, 8}, {2, 2}
            });
        }    
    
        @Test
        public void testAdd() {
            assertEquals(value1, value2);
        }
    }
    

    Note:
    Both the original code in the question and this code test the equality between two hard-coded values instead of any logic from an actual implementation, so their value is somewhat questionable.
    This example just shows how to convert the code in the question to a parameterized test, and does not handle any logic changes.