Search code examples
javajunitannotationsjunit5

Is there a way to log the JUnit5 @Disabled reason value during a full test run?


I've exhausted a variety of search terms I thought would net me a direct answer, and I have found nothing definitive (even in JUnit5's docs). Is there a way to retrieve the "reason" from a @Disabled annotation in JUnit5? Current approach I am working with:

  1. Create an extension class that implements BeforeEachCallback
  2. Cycle through test method annotations looking for instanceof Disabled
  3. On finding that annotation, retrieve the reason value and log

The logic is there and works on a single specific test run, but when I run a full test class the method is skipped over in the BeforeEachCallback's @Override. So I assume JUnit does some magic that excludes it from the test run lifecycle as a whole. If that is the case, is there a way to retrieve that annotation and log it at an earlier point than BeforeEachCallback? I've also tried BeforeAllCallback, but wasn't able to access the method children of the test class.


Solution

  • Extension APIs such as BeforeEachCallback are not invoked for a test method that is disabled via an ExecutionCondition.

    JUnit Jupiter 5.4 introduced a new TestWatcher extension API that has a testDisabled() callback for disabled tests. You can find out more about it in the User Guide.