views:

57

answers:

2

hello, 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

A: 
ex.printStackTrace(new PrintStream(new FileOutputStream("/path/to/file.txt")));

(alternatively you can use PrintWriter and FileWriter)

Bozho
Bozho, thnaks for getting back to me in my test I do not use try/catch bloc to use "ex" I only do some actions then call assertEquals("wwith its arg") to verify that what I'm doing is correct
laura
+1  A: 

Laura, 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);
    }
Dave
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
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
so what are the good practice ?
laura
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
ok Dave, so the junit test can have the following: @BeforeClass public static void setUpBeforeClass() throws Exception { startappli();}
laura
@Test public void test1(){try{clickButton(); assertEquals(true,true);}catch(AssertionError ex){//something}
laura
@AfterClass public static void AfterTest(){CloseAppli();}
laura
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
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
If you want the method to be called before every test defined in the class you should use @Before and @After instead.
Dave
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
No those annotations are not always useful. I would even discourage using @BeforeClass because this makes your tests dependend of eachother.
Dave
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
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
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
@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