开发者

Detect Failure or Error of Junit Test in @After method

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 wit开发者_JAVA技巧houtFailure = 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!?


If you are lucky enough to be using JUnit 4.9 or later, TestWatcher will do exactly what you want.

Share and Enjoy!


I extend dsaff's answer to solve the problem that a TestRule can not execute some code snipped between the execution of the test-method and the after-method. So with a simple MethodRule one can not use this rule to provide a success flag that is use in the @After annotated methods.

My idea is a hack! Anyway, it is to use a TestRule (extends TestWatcher). A TestRule will get knowledge about failed or success of a test. My TestRule will then scan the class for all Methods annotated with my new AfterHack annotations and invoke that methods with a success flag.


AfterHack annotation

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;    
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(METHOD)
public @interface AfterHack {}

AfterHackRule

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class AfterHackRule extends TestWatcher {

    private Object testClassInstance;
    public AfterHackRule(final Object testClassInstance) {
        this.testClassInstance = testClassInstance;
    }

    protected void succeeded(Description description) {
        invokeAfterHackMethods(true);
    }

    protected void failed(Throwable e, Description description) {
        invokeAfterHackMethods(false);
    }

    public void invokeAfterHackMethods(boolean successFlag) {
        for (Method afterHackMethod :
                    this.getAfterHackMethods(this.testClassInstance.getClass())) {
            try {
                afterHackMethod.invoke(this.testClassInstance, successFlag);
            } catch (IllegalAccessException | IllegalArgumentException
                     | InvocationTargetException e) {
                throw new RuntimeException("error while invoking afterHackMethod " 
                          + afterHackMethod);
            }
        }
    }

    private List<Method> getAfterHackMethods(Class<?> testClass) {
        List<Method> results = new ArrayList<>();            
        for (Method method : testClass.getMethods()) {
            if (method.isAnnotationPresent(AfterHack.class)) {
                results.add(method);
            }
        }
        return results;
    }
}

Usage:

public class DemoTest {

    @Rule
    public AfterHackRule afterHackRule = new AfterHackRule(this);

    @AfterHack
    public void after(boolean success) { 
        System.out.println("afterHack:" + success);
    }

    @Test
    public void demofails() {
        Assert.fail();
    }

    @Test
    public void demoSucceeds() {}
}

BTW:

  • 1) Hopefully there is a better solution in Junit5
  • 2) The better way is to use the TestWatcher Rule instead of the @Before and @After Method at all (that is the way I read dsaff's answer)

@see


I don't know any easy or elegant way to detect the failure of a Junit test in an @After method.

If it is possible to use a TestRule instead of an @After method, one possibility to do it is using two chained TestRules, using a TestWatcher as the inner rule.

Example:

    package org.example;

    import static org.junit.Assert.fail;

    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.ExternalResource;
    import org.junit.rules.RuleChain;
    import org.junit.rules.TestRule;
    import org.junit.rules.TestWatcher;
    import org.junit.runner.Description;

    public class ExampleTest {

      private String name = "";
      private boolean failed;

      @Rule
      public TestRule afterWithFailedInformation = RuleChain
        .outerRule(new ExternalResource(){
          @Override
          protected void after() {
            System.out.println("Test "+name+" "+(failed?"failed":"finished")+".");
          }      
        })
        .around(new TestWatcher(){
          @Override
          protected void finished(Description description) {
            name = description.getDisplayName();
          }
          @Override
          protected void failed(Throwable e, Description description) {
            failed = true;
          }      
        })
      ;

      @Test
      public void testSomething(){
        fail();
      }

      @Test
      public void testSomethingElse(){
      }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜