How make JUnit print assertion results

82,416

Solution 1

First, you have two issues not one. When an assertion fails, an AssertionError exception is thrown. This prevents any assertion past this point from being checked. To address this you need to use an ErrorCollector.

Second, I do not believe there is any way built in to JUnit to do this. However, you could implement your own methods that wrap the assertions:

public static void assertNotNull(String description, Object object){
     try{
          Assert.assertNotNull(description, object);
          System.out.println(description + " - passed");
     }catch(AssertionError e){
          System.out.println(description + " - failed");

        throw e;
     }
}

Solution 2

All the assertXXX methods have a form that allows for displaying a String on error:

assertNotNull("exists a2", p); // prints "exists a2" if p is null

There is no particular value in printing a message on success.

EDIT

Junit typically provides 2 forms of an assert. To follow the example above, you can test for a null value in 1 of 2 ways:

assertNotNull(p)

or

assertNotNull("my message on failure", p)

The framework will print the error messages with no other effort required by you (it's provided by the framework).

To test for exceptions you would use the following pattern:

try{
    someCall();
catch(Exception e){
    fail(): // exception shouldn't happen, use assertTrue(true) if it should
}

Again, there are versions of these methods for adding a message

Check the API

Solution 3

One last resort option is to pair each assert with a corresponding System.out.println, though obviously that is less than ideal. Still, it will solve the problem if all else fails.

Share:
82,416
Petr Přikryl
Author by

Petr Přikryl

Updated on June 29, 2020

Comments

  • Petr Přikryl
    Petr Přikryl almost 4 years

    How can I get the results of my JUnit assertions to be printed [to standard output]?

    I have some tests like this:

    @Test
    public void test01()
    {
        Position p = getPositionAt('a', 1);
        assertNotNull("a1 exists", p);
        assertNotNull("figure exists a1", p.getFigure());
        
        p = getPositionAt('a', 2);
        assertNotNull("exists a2", p);
        assertNull("figure exists a2", p.getFigure());
        
        p = getPositionAt('b', 1);
        assertNotNull("exists b1", p);
        assertNull("figure exists b1", p.getFigure());
    }
    

    This is the printed output format I am hoping to get:

    a1 exists -success
    figure exists a1 -success
    exists a2 -success
    figure exists a2 -succcess
    exists b1 -succcess
    figure exists b1 -failed
    

    Is there way to do this using runners and suites? Or does there exist any assertSuccess(), assertFailed() methods?

  • Petr Přikryl
    Petr Přikryl about 11 years
    Ok, so I can move message in front of assert, but how to catch if assert passed or failed to print result message?
  • John B
    John B about 11 years
    catch the exception as shown in my answer but if you do so, make sure to rethrow the exception (or trap it in an error collector) otherwise you will get false positives
  • JoachimR
    JoachimR over 8 years
    shouldn't be the assertNotNull within the function be TestCase.assertNotNull ?
  • Brian Reinhold
    Brian Reinhold about 7 years
    I do assertTrue("some message", assertion); and the message still does not print.
  • AlikElzin-kilaka
    AlikElzin-kilaka over 6 years
    Please do not ignore exceptions in tests. Just throw them or rethrow with more info. The fail() call in exception clause is problematic. Just propagate the exception... Do not write your own catch blocks that exist only to fail a test
  • Romski
    Romski over 6 years
    @AlikElzin-kilaka thanks for the comment, Im surprised this answer still gets attention! I don't disagree with your comment, but would add that testing for an expected exception is part of a passing test, and so is valid. Also, there are valid uses cases for calling fail(). I agree that my example could be better.
  • Anupam Haldkar
    Anupam Haldkar almost 4 years
    why we cannot print assertEqual statement? I am getting error.
  • Anupam Haldkar
    Anupam Haldkar almost 4 years
    That means we have no option to check what does assertEqual() return ? that's why we are enable to print. Right?
  • cellepo
    cellepo almost 4 years
    It does not appear any of the possibilities in the last comment by Romski, are actually desired in this case. So the possibilities are valid, but at least a little bit out of scope here.
  • cellepo
    cellepo almost 4 years
    @AnupamHaldkar the result of the test will show the result of any assertions - if any fail within a unit test, that individual test fails: The result of that test's success/failure is visible wherever the tests results output to (which, depending on the context of how you run your tests, could be {IDE display, log file, terminal output, standard output}). This touches a bit on whether or not a principle of one assertion per unit test is adhered to, but that is tangential here.
  • cellepo
    cellepo almost 4 years
    @AnupamHaldkar you need to either find help in another Answer that covers the specifics of your code, or Ask a new Question with those specifics (if not already covered by an existing Question). We can't help you if you don't tell us what you are doing. It is also not clear why you think the print is not happening - it probably is happening, and you just need to figure out where it is printing to.
  • cellepo
    cellepo almost 4 years
    @AnupamHaldkar to be clear, the answer to your two questions here is, "no, incorrect". You might need to learn about printing to standard output in general (although that is not the only place to configure/find unit test results).
  • cellepo
    cellepo almost 4 years
    @AnupamHaldkar where do you see anything else you print from a Java program? That will probably guide you how/where to see printing from tests. But see my answer, as printing is probably what you want to do in the first place (like other comments allude to).