Search code examples
javajunitstatic-assert

How to get failure trace with Junit4


in my Junit test, I use usually "AssertEquals" and when the test fails, the trace is properly displayed in the Failure trace of JUnit/eclipse I would like to know how to get these trace to show it in a file?

@Test 
  public void testButtons() { 
       SelectionButton().ButtonFile();
       assertEquals("selected button should be edit",FILE.EDIT,File.getSelectedItem);
  } 

how could I print/redirect the assert failure trace in a file ? thanks


Solution

  • The assertEquals method of JUnit throws an AssertionError without a message on error. If you want to log more information on the failure you will have to catch the AssertionError like in:

    try{
        assertEquals(true, true);
    }catch (AssertionError ex) {
        //Do Something
        throw(ex);
    }