Araújo Filho Araújo Filho - 11 months ago 47
Java Question

How to get an output from JUnit assertEquals() method and put it into a variable?

I have a JUnit test case here to test a simple class. Basically the class contains just one method called 'sum' that returns the sum of two numbers. To test if this is right I use the following method:

Assert.assertEquals(2, my_object.sum(1, 2));
The result displays in a tab on eclipse, in a section called "Failure Trace". The message says:
junit.framework.AssertionFailedError: expected:<2> but was:<3>
. Is it possible to get this message and put it into a String variable?


You have to catch AssertionError when your condition fails.

Try this code:

    public void myTest() throws Exception{
        String assertionError = null;
        try {
        catch (AssertionError ae) {
            assertionError = ae.toString();



java.lang.AssertionError: expected:<2> but was:<3>