Stop 14 · Spring Boot & Backend DevelopmentTopics · 11/13

Testing Spring Boot Apps

Part of the Java Roadmap

In short

Tests let you change code on Friday evening without fear. Spring Boot gives you fast slice tests for each layer and full integration tests with a real database, so bugs show up on your laptop, not in production.

Spring Boot supports fast slice tests like @WebMvcTest with MockMvc for controllers and @DataJpaTest for repositories, plus @SpringBootTest for full integration tests, often with Testcontainers for a real database.

The jaadu with a clipboard inspects a shiny car on a factory test track, tapping the brakes on a separate test rig while a mechanic checks the engine.

Think of it like this

Checking a car before delivery

At a car factory in Chennai, the brakes are tested on their own rig, the engine on another, and only then does the full car go on a test drive. @WebMvcTest checks the controller alone, @DataJpaTest checks the repository alone, and @SpringBootTest is the full test drive.

Watch it happen

Slice test vs full test

@WebMvcTest

  • Loads only web layer beans
  • Services replaced with @MockitoBean
  • Runs in about a second
  • Tests status codes and JSON

@SpringBootTest

  • Loads the whole application
  • Real services and database
  • Slower, use Testcontainers
  • Tests the full flow

Write many fast slice and unit tests, and a few full integration tests for the most important flows.

The big ideas

What you need to know

Unit tests first

Services with constructor injection can be tested with JUnit 5 and Mockito, no Spring at all.

@WebMvcTest + MockMvc

Sends fake HTTP requests to one controller and checks status, headers and JSON with jsonPath.

@DataJpaTest

Loads JPA and repositories only, and rolls back after each test.

Testcontainers

Starts a real PostgreSQL in Docker for the test, so you test against the same database you deploy on.

See it in code

Walk through the code

java
1import org.junit.jupiter.api.Test;2import org.springframework.beans.factory.annotation.Autowired;3import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;4import org.springframework.test.context.bean.override.mockito.MockitoBean;5import org.springframework.test.web.servlet.MockMvc;6 7import static org.mockito.Mockito.when;8import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;9import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;10 11@WebMvcTest(ProductController.class)12class ProductControllerTest {13 14    @Autowired15    MockMvc mvc;16 17    @MockitoBean18    ProductService service;19 20    @Test21    void returnsProduct() throws Exception {22        when(service.get(7L)).thenReturn(new ProductResponse(7L, "Kurta", 899));23 24        mvc.perform(get("/api/products/7"))25           .andExpect(status().isOk())26           .andExpect(jsonPath("$.name").value("Kurta"))27           .andExpect(jsonPath("$.price").value(899));28    }29 30    @Test31    void missingProductGives404() throws Exception {32        when(service.get(99L)).thenThrow(new ProductNotFoundException(99L));33 34        mvc.perform(get("/api/products/99"))35           .andExpect(status().isNotFound());36    }37}

1/5Load only the web layer and this one controller.

@MockitoBean replaced @MockBean in Spring Boot 3.4. On older versions use @MockBean from spring-boot-test.

Save yourself the pain

Common mistakes

Oops

Using @SpringBootTest for every single test.

Do this instead

It starts the whole app each time. Use unit tests and slices for most cases, and keep full tests for key flows.

Oops

Testing against H2 while production runs PostgreSQL.

Do this instead

SQL and behaviour differ. Use Testcontainers with the same database for repository and integration tests.

Oops

Only testing the happy path.

Do this instead

Add tests for validation errors, missing records and forbidden access. That's where bugs hide.

Your mission

Cover the products API

~45 min

Quick check

Which annotation loads only the MVC layer for testing a controller?

Go deeper

  1. 1Test a controller with @WebMvcTest and MockMvc, mocking the service.
  2. 2Test a repository query with @DataJpaTest.
  3. 3Run one full integration test against a PostgreSQL Testcontainer.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge