JUnit5 - How to get test result in AfterTestExecutionCallback

bugs_ picture bugs_ · Mar 10, 2017 · Viewed 8.2k times · Source

I write JUnit5 Extension. But I cannot find way how to obtain test result.

Extension looks like this:

import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.TestExtensionContext;

public class TestResultExtension implements AfterTestExecutionCallback {
    @Override
    public void afterTestExecution(TestExtensionContext context) throws Exception {
        //How to get test result? SUCCESS/FAILED
    }
}

Any hints how to obtain test result?

Answer

Obscured Clouds picture Obscured Clouds · Oct 19, 2017

This work for me:

public class RunnerExtension implements AfterTestExecutionCallback {

    @Override
    public void afterTestExecution(ExtensionContext context) throws Exception {
        Boolean testResult = context.getExecutionException().isPresent();
        System.out.println(testResult); //false - SUCCESS, true - FAILED
    }
}

@ExtendWith(RunnerExtension.class)
public abstract class Tests {

}