How to get the result of a JUnit assertEquals test and why in a String type variable?

0

I'm having a JUnit test case to test a simple class. The class to be tested contains only one "sum" method that adds two numbers. To test if it is correct, I use the following method: Assert.assertEquals(2, meu_objeto.sum(1, 2)); The result appears in a JUnit tab in the eclipse, in a part called Failure Trace. The message says: junit.framework.AssertionFailedError: expected:<2> but was:<3> Is it possible to get this message and put it inside a String variable?

    
asked by anonymous 25.04.2016 / 18:53

1 answer

2
 @Test
    public void myTest() throws Exception{
        String assertionError = null;
        try {
            Assert.assertEquals(2,3);
        }
        catch (AssertionError ae) {
            assertionError = ae.toString();
        }
        System.out.println(assertionError);

    }

I got the answer in StackOverFlow in English, answered by a user named 'Unknown'.

    
26.04.2016 / 13:55