@ExtendWith registers one or more JUnit Jupiter extensions on a test class, method, field or parameter. Extensions add behaviour around tests, such as dependency injection, mocking, conditional execution and setup or teardown, and @ExtendWith is the declarative way to switch them on. It replaces the JUnit 4 @RunWith and @Rule mechanisms with a single model.
Common examples are @ExtendWith(MockitoExtension.class) for Mockito mocks and @ExtendWith(SpringExtension.class) for Spring's test context.
@ExtendWith with Mockito@ExtendWith with SpringJUnit 5 has one extension API instead of runners, rules and class rules. An extension is a class that implements one or more callback interfaces from org.junit.jupiter.api.extension. JUnit calls those callbacks at defined points in the test lifecycle.
| Interface | Purpose |
|---|---|
BeforeAllCallback, AfterAllCallback | Run once around all tests in a class |
BeforeEachCallback, AfterEachCallback | Run around every test method |
ParameterResolver | Inject arguments into test and lifecycle methods |
ExecutionCondition | Enable or disable tests dynamically |
TestInstancePostProcessor | Post-process the test instance, for example to inject fields |
TestExecutionExceptionHandler | Handle or translate exceptions thrown by a test |
TestWatcher | Observe test results |
@ExtendWith takes an array of extension classes, and it is a repeatable annotation.
@ExtendWith with MockitoThe most common real-world use is Mockito.
MockitoExtension creates the @Mock objects before each test, injects them into @InjectMocks fields and, by default, uses strict stubs, so an unused stubbing fails the test. You write no setup code.
@ExtendWith with SpringSpringExtension loads the Spring ApplicationContext and injects beans into the test. Annotations such as @SpringBootTest, @WebMvcTest and @DataJpaTest are already meta-annotated with @ExtendWith(SpringExtension.class) in Spring Boot 2.1 and later, so you do not repeat it on those tests.
An extension is plain Java. This one times each test.
Apply it with the annotation:
The ExtensionContext store keeps per-test state so the extension itself stays stateless and safe for parallel execution.
A ParameterResolver lets tests declare arguments that your extension provides.
A test then writes @Test void works(Clock clock) and receives a fixed clock.
| Way | When to use it |
|---|---|
@ExtendWith on a class, method, field or parameter | Declarative, the default choice |
@RegisterExtension on a field | When the extension needs constructor arguments or programmatic configuration |
Automatic detection through ServiceLoader | When enabled with junit.jupiter.extensions.autodetection.enabled=true, for extensions on the classpath |
You can also combine several extensions in a custom composed annotation:
Extensions registered at class level apply to every test inside, and extensions declared on a method only affect that test. Extensions are inherited by subclasses.
@RunWith in JUnit 5?@ExtendWith. For example, @RunWith(MockitoJUnitRunner.class) becomes @ExtendWith(MockitoExtension.class), and @RunWith(SpringRunner.class) becomes @ExtendWith(SpringExtension.class).
Yes. Pass an array, @ExtendWith({A.class, B.class}), or repeat the annotation. They are invoked in the order they are registered.
@ExtendWith needed for @SpringBootTest?No. Spring Boot's test annotations already include SpringExtension, so adding it again is redundant.
@RegisterExtension instead?Use it when the extension must be created programmatically, for example with a builder or constructor arguments, or when you need to read its state from the test through a field.
@ExtendWith plugs reusable behaviour into JUnit 5 tests through the Jupiter extension API. Use it to enable Mockito and Spring support, to write your own lifecycle callbacks, parameter resolvers and conditions, and to replace the @RunWith and @Rule constructs from JUnit 4 with a single, composable mechanism.