SpringExtension is a JUnit extension that connects tests with Spring’s application context. It manages context startup and shutdown and makes configured Spring beans available to tests. section 1.1 section 3.1
SpringExtension supplies the infrastructure behind declarative Spring Boot testing:
- Registers custom behavior across JUnit test lifecycle phases. section 3.1
- Builds and runs a test application through the Spring TestContext Framework. section 3.1
- Supports dependency injection into test classes through @Autowired. section 3.1
- Lets annotations express configuration instead of requiring imperative application-startup calls. section 1.1
Registration through @SpringBootTest
@SpringBootTest includes @ExtendWith(SpringExtension.class), so Spring Boot tests receive SpringExtension behavior without registering the extension separately. Its surrounding configuration can select properties, classes, web-environment behavior, and main-method usage. section 3.1
Listing 3.1 Simplified code for @SpringBootTest extension.
@ExtendWith(SpringExtension.class) #1
@BootstrapWith(SpringBootTestContextBootstrapper.class) #2
public @interface SpringBootTest {
// Below are generic Spring configuration options, exposed by @SpringBootTest
String[] properties() default {}; #3
Class<?>[] classes() default {}; #3
// Below are Spring Boot specific configuration options
WebEnvironment webEnvironment() default WebEnvironment.MOCK; #3
UseMainMethod useMainMethod() default UseMainMethod.NEVER; #3
String[] args() default {}; #3
}
Inspect the simplified annotation structure showing SpringExtension registration alongside Spring Boot’s test-context bootstrapper. section 3.1
