SpringExtension is the JUnit Jupiter extension that connects the Spring TestContext Framework to JUnit 5. Registered with @ExtendWith(SpringExtension.class), it loads an ApplicationContext for your test class, injects Spring beans into test instances and test method parameters, and applies Spring test features such as @Transactional, @Sql and @DirtiesContext. It replaces SpringRunner and @RunWith, which belong to JUnit 4.
You rarely need to write the annotation yourself, because @SpringBootTest, the @WebMvcTest and @DataJpaTest slice annotations, and @SpringJUnitConfig already include it.
JUnit 5 has an extension model with callbacks around the test lifecycle. SpringExtension implements several of them and delegates to the Spring TestContextManager:
ApplicationContext and performs dependency injection on the instance, so @Autowired, @Value and @MockitoBean fields are filled.@Sql scripts.ParameterResolver, so constructor and method parameters can be injected from the context.The context cache is important for speed: contexts are cached by their configuration, so test classes with identical configuration share a single started context.
For a plain Spring application, combine the extension with a configuration:
@ContextConfiguration tells the extension which configuration class to load. The GreetingService bean is then injected into the test field.
The shortcut @SpringJUnitConfig(GreetingTest.Config.class) is the same as @ExtendWith(SpringExtension.class) plus @ContextConfiguration.
With Spring Boot you normally write only the Boot annotation, because it already registers the extension:
Adding @ExtendWith(SpringExtension.class) on top is redundant here. Since Spring Boot 2.1 these annotations are meta-annotated with it:
| Annotation | Purpose |
|---|---|
@SpringBootTest | Full application context |
@WebMvcTest | MVC slice with MockMvc |
@DataJpaTest | JPA slice with an embedded or test database |
@SpringJUnitConfig | Plain Spring configuration |
@SpringJUnitWebConfig | Spring configuration with a web application context |
Because SpringExtension is a ParameterResolver, tests can use constructor injection and injected method parameters, which keeps fields final:
The @Autowired annotation on a parameter is needed so that Spring, not JUnit's default resolution, supplies the argument. Setting spring.test.constructor.autowire.mode=all in junit-platform.properties removes that requirement for constructors.
Test listeners registered through the extension add useful behaviour:
@Transactional on a test wraps each method in a transaction that rolls back afterwards.@Sql runs scripts before or after a test.@DirtiesContext discards the cached context. Use it sparingly because rebuilding contexts slows the suite.@TestPropertySource and @ActiveProfiles change the configuration for a test class, which creates a new cache entry.| JUnit 4 | JUnit 5 |
|---|---|
@RunWith(SpringRunner.class) | @ExtendWith(SpringExtension.class) |
@Before / @After | @BeforeEach / @AfterEach |
@Rule (SpringClassRule) | Extension model |
org.junit.Test | org.junit.jupiter.api.Test |
Use the Jupiter Test import. Mixing org.junit.Test with SpringExtension is a common reason tests are silently skipped or injection stays null.
No. @SpringBootTest and the Spring Boot slice annotations already include it. Add it manually only for plain Spring tests that use @ContextConfiguration.
Usually the extension is not registered, the test imports the JUnit 4 @Test, or the test class is instantiated manually. Check the import and the annotations.
It is meant for integration tests that need a Spring context. For pure unit tests, construct the class directly and mock collaborators with Mockito, which is faster.
By their full configuration key: classes, profiles, properties and mocks. Identical keys share one context, so keeping test configurations uniform makes the suite faster.
SpringExtension integrates the Spring TestContext Framework into JUnit 5 by loading and caching application contexts, injecting beans into tests, and enabling transactional, SQL and property-based test features. Use @SpringBootTest or a slice annotation in Spring Boot projects, @SpringJUnitConfig in plain Spring, and register the extension directly only when you need that fine control.