How to make logging in Junit, if the test is NOT passed. Roughly speaking, how to make the code assertTrue(res); after assertTrue(res);

    3 answers 3

    No That's what he and assert. If you need to do something, then do the test with hands like

     assertTrue("my message tra-la-la", res); 


     if (!res) { // build message fail(message ...); } 
      1. The simplest is to launch the console runner of tests to redirect the output to a file.
      2. If something more complicated is needed, then it will be necessary to redefine the Runner class and implement the freeware of event handling that occurs during the test run.

        Why not use ordinary try..catch?

        I give an example

         AssertionError assertionError = null; try{ //test code here }catch(AssertionError aEr){ assertionError = aEr; }finally{ if(assertionError == null){ // log success }else{ // log fail throw assertionError; } } 

        Redefining Runner is also an option, but in essence it will do absolutely the same thing. It makes sense to do so, if this is not an isolated case: it can be used for other tests.