liveBook · Manning

Spring Boot test

As explained in Testing Spring Boot Applications

Spring Boot's testing support for exercising application code together with Spring-managed components, configuration, and framework behavior.

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

How Testing Spring Boot Applications covers it

How deep it goes
Works through annotation-driven application startup and MVC assertions, while deferring deeper MockMvcTester detail and warning that abstractions can become limiting.
Examples
Uses an HTTP GET / test asserting status and HTML, plus API tests checking JSON and converted Java objects.
What it assumes
Assumes familiarity with Spring Framework, Spring Boot, and automated testing rather than teaching general testing principles from zero.
What you can do after
Apply it via @SpringBootTest and MockMvcTester to verify application behavior, while choosing unit tests or a real server when appropriate.
How it connects
Connects integration tests with application contexts, dependency injection, MockMvcTester, configuration, unit tests, and end-to-end tests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.