This version is still in development and is not considered stable yet.
For the latest stable version, please use JUnit 6.0.1!

Intercepting Invocations

InvocationInterceptor defines the API for Extensions that wish to intercept calls to test code.

The following example shows an extension that executes all test methods in Swing’s Event Dispatch Thread.

An extension that executes tests in a user-defined thread
public class SwingEdtInterceptor implements InvocationInterceptor {

	@Override
	public void interceptTestMethod(Invocation<Void> invocation,
			ReflectiveInvocationContext<Method> invocationContext,
			ExtensionContext extensionContext) throws Throwable {

		AtomicReference<Throwable> throwable = new AtomicReference<>();

		SwingUtilities.invokeAndWait(() -> {
			try {
				invocation.proceed();
			}
			catch (Throwable t) {
				throwable.set(t);
			}
		});
		Throwable t = throwable.get();
		if (t != null) {
			throw t;
		}
	}
}
Accessing the test-scoped ExtensionContext

You may override the getTestInstantiationExtensionContextScope(…​) method to return TEST_METHOD to make test-specific data available to your extension implementation of interceptTestClassConstructor or if you want to keep state on the test method level.