Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make JUnit print assertion results

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?

like image 395
Petr Přikryl Avatar asked Apr 10 '13 12:04

Petr Přikryl


People also ask

How does JUnit check return value?

You want to test, first you have to prepare data to test, the input value, the expected value => call test function with input value => get the actual value return by function under test => assert the expected value with the actual value.

How many methods for asserting the outputs are there in JUnit?

Assertions are utility methods to support asserting conditions in tests; these methods are accessible through the Assert class, in JUnit 4, and the Assertions one, in JUnit 5.

How do you use assertTrue in JUnit?

assertTrue() If you wish to pass the parameter value as True for a specific condition invoked in a method, then you can make use of the. JUnit assertTrue(). You can make use of JUnit assertTrue() in two practical scenarios. By passing condition as a boolean parameter used to assert in JUnit with the assertTrue method.


1 Answers

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;      } } 
like image 150
John B Avatar answered Sep 19 '22 14:09

John B