-
Notifications
You must be signed in to change notification settings - Fork 0
Spring Testing
ContextConfiguration - A class annotated with this can be considered a lightweight integration test. It is used to target loading specific configuration(s) for use in a test. It is a general Spring feature, and not specific to Spring Boot.
It does involve spring initialization so should be avoided where possible when writing tests, but it is less of an overhead that using @SpringBootTest.
SpringBootTest - A class annotated with this is an integration test. It will load the entire spring Application Context, and consequently will likely include a significant startup cost.
Integration tests should be run judiciously during development. Although it is essential to run them during the build pipeline, particularly before deployment in any environment, they should not be run routinely during day-to-day development.
The SpringBootTest annotation provides the following features:
- Auto Configuration
- Application Properties Loading
- Component Scanning
- Embedded Web Server Support
When adding test dependencies to a Maven build, it is important to remember to set the scope attribute to test to ensure no test libraries getting packaged in the build.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
For Gradle builds, the same is true, but we using the testImplementation directive instead.
dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.mockito:mockito-core:5.12.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
}