Test lifecycle

There are a set of extension points aimed at controlling the life cycle of tests. First of all, the TestInstancePostProcessor can be used to execute some logic after the test instantiation. After that, there are different extensions which control the pre-test stage:

  • The BeforeAllCallback defines the logic executed before all tests.
  • The BeforeEachCallback defines the logic executed before a test method.
  • The BeforeTestExecutionCallback defines the logic executed immediately before a test method.

Similarly, there are extensions to control the post-test phases:

  • The AfterAllCallback defines the logic executed after all tests.
  • The AfterEachCallback defines the logic executed after a test method.
  • The AfterTestExecutionCallback defines the logic executed immediately after a test method.

In between the Before* and After* callbacks, there is an extension that provides a way for collecting exceptions: the TestExecutionExceptionHandler.

All these callbacks, and their order in the test life cycl are depicted in the following picture:

Lifecycle of extension callbacks

Let's see an example. We created an extension called IgnoreIOExceptionExtension, which implements TestExecutionExceptionHandler. In this example, the extension checks whether or not the exception is IOException. If so, the exception is discarded:

package io.github.bonigarcia;

import java.io.IOException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;

public class IgnoreIOExceptionExtension
implements TestExecutionExceptionHandler {

@Override
public void handleTestExecutionException(ExtensionContext context,
Throwable throwable) throws Throwable {
if (throwable instanceof IOException) {
return;
}
throw throwable;
}

}

Consider the following test class, which contains two tests (@Test). The first one is annotated with @ExtendWith and our custom extension (IgnoreIOExceptionExtension):

package io.github.bonigarcia;

import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

public class ExceptionTest {

@ExtendWith(IgnoreIOExceptionExtension.class)
@Test
public void firstTest() throws IOException {
throw new IOException("IO Exception");
}

@Test
public void secondTest() throws IOException {
throw new IOException("My IO Exception");
}

}

When executing this test class, the first test is succeeded due to the fact that the IOException has been internally handled by our extension. On the other hand, the second will fail since that exception is not handled.

The execution of this test class in the console can be seen in the next screenshot. Note that we select the test to be executed using the Maven command mvn test -Dtest=ExceptionTest:

Output of ignore exception example
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.250.11