Detect Failure or Error of Junit Test in @After method

Viewed 22634

Is there a way in JUnit to detect within an @After annotated method if there was a test failure or error in the test case?

One ugly solution would be something like that:

boolean withoutFailure = false;

@Test
void test() {
  ...
  asserts...
  withoutFailure = true;
}

@After
public void tearDown() {
   if(!withoutFailuere) {
      this.dontReuseTestenvironmentForNextTest();
   }
}

This is ugly because one need to take care of the "infrastructure" (withoutFailure flag) in the test code.

I hope that there is something where I can get the test status in the @After method!?

3 Answers
Related