Spring Boot testing support combines test-scoped dependencies, Spring integration, and utilities for exercising application code with Spring-managed components. It is especially useful for integration tests: tests that check whether application classes work together and integrate correctly with capabilities such as the web or data layer. It sits between isolated unit tests and tests against a fully deployed system. section 1.1 section 1.2
There is no separate official project called “Spring Boot Tests.” The core starter bundles a test runner, assertions, mocking, and Spring testing support. Web applications can use dedicated MVC or WebFlux test starters. Keep these dependencies in test scope so they are unavailable to production code. section 1.1 section 1.6
Annotating a test class with @SpringBootTest lets Spring Boot detect the application configuration and manage a test application’s lifecycle. Tests can access beans with @Autowired, change configuration, or replace beans when an external service should not be called. This avoids manually starting the application, but a full application context is not necessary for every test. section 3.0 section 1.6
Listing 2.2 Verifying that the application responds to HTTP queries.
@SpringBootTest #1
@AutoConfigureMockMvc #2
class ExampleTests {
@Autowired #2
private MockMvcTester mvc; #2
@Test
void indexPageLoads() {
// 1. Arrange
// We do not need to set up anything in particular
// 2. Act
var response = mvc.get() #3
.uri("/")
.exchange();
// 3. Assert
assertThat(response) #4
.hasStatus(HttpStatus.OK)
.bodyText()
.contains("<h1>Weather App</h1>");
}
}
A focused MVC test pairs @SpringBootTest with @AutoConfigureMockMvc. Spring supplies a MockMvcTester bean; the test requests GET / without specifying a host or port, then checks the status and rendered HTML with AssertJ. section 2.3
Choose the smallest setup that verifies the behavior you care about:
- Use the mock MVC stack and MockMvcTester for most web requests and API assertions. Responses can be checked as text, JSON, or converted Java objects. section 4.1 section 4.6
- Use @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) with RestTestClient or WebTestClient when the behavior depends on a real web server, such as error dispatch or session handling. section 4.6
- Use focused unit tests without starting Spring Boot for complex conditional logic that can be checked within one service. Full-context tests make such cases harder to set up and repeat. section 2.7 section 1.6
For HTML or JavaScript behavior, HtmlUnit can run against mock MVC on the JVM; Selenium uses a full browser and better suits JavaScript-heavy applications. Spring Boot tests remain useful for application-level scenarios, not a replacement for every other testing approach. section 4.6 section 1.6
