0

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

laura
  • 155
  • 3
  • 13
  • I tried from eclipse to set an output file ,but xhen the test fails, the assertion trace is only displayed in the failure trace view and not in the file. – laura Oct 06 '10 at 08:29

1 Answers1

2

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);
}
cello
  • 5,356
  • 3
  • 23
  • 28
Dave
  • 1,417
  • 14
  • 23
  • it is working for me, thanks. so we can use try/catch with assert? so I can use catch (AssertionError ex) for all assertions ? – laura Oct 06 '10 at 09:42
  • well you can do this, but it is in general not good practice because if you do not throw the exception at the end of the catch part, the test will falsely pass. – Dave Oct 06 '10 at 09:48
  • 1
    Normally your test is not supposed to fail right? When you write an assert(expected, actual) (of whatever type) you expect the actual part to be the same as the expected part. If this is not the case the part of the code you are testing is wrong and you should look over there and not try to add more logging in your tests. Unit tests are not supposed to give you stacktraces or debug info, Eclipse does this for you anyway to start with the debugging more easily. – Dave Oct 06 '10 at 10:06
  • ok Dave, so the junit test can have the following: @BeforeClass public static void setUpBeforeClass() throws Exception { startappli();} – laura Oct 06 '10 at 10:38
  • @Test public void test1(){try{clickButton(); assertEquals(true,true);}catch(AssertionError ex){//something} – laura Oct 06 '10 at 10:41
  • @AfterClass public static void AfterTest(){CloseAppli();} – laura Oct 06 '10 at 10:41
  • sorry for the format, so is it a good junit test ? and could I call the same methode @afterClass and @BeforeClass from the other tests ? – laura Oct 06 '10 at 10:43
  • Yes that's an acceptable test. You should not call a method annotated with AfterClass or BeforeClass yourself. If you want to call the method yourself why not drop the annotation? – Dave Oct 06 '10 at 10:58
  • If you want the method to be called before every test defined in the class you should use @Before and @After instead. – Dave Oct 06 '10 at 11:00
  • so the annotations @Before,@After,@AfterClass and @BeforeClass aren't always useful? but if I want to open open the appli(my software to test) and close it before and after all @Test in the same class, I have to add @AfterClass and @BeforeClass, right ? otherwise I use @after and @Before if I want to do it before and after each @Test,right ? – laura Oct 06 '10 at 13:53
  • No those annotations are not always useful. I would even discourage using @BeforeClass because this makes your tests dependend of eachother. – Dave Oct 06 '10 at 14:12
  • but I have to launch the appli before each test , so should I use @Before ? or may be call the function that launches the appli inside @Test ? – laura Oct 06 '10 at 14:26
  • or could I for example create a class where I call launchAppli() and closeit() respectively in @BeforeClass and @AfterClass and I make my tests class extends this class ? – laura Oct 06 '10 at 16:10
  • Yes, if you want to launch the application before each test you should use @Before. Yes, it is allowed to use inheritance with test classes. – Dave Oct 07 '10 at 06:45
  • @Before before each @Test and not once in the testClass ,right ? thanks Dave for answers. should I do something to to accept answers or vote via stackoverflow ? – laura Oct 07 '10 at 07:18