Medium15 minSpring Boot Fundamentals
UpdatedAug 5, 2026
Edit

Spring Boot Testing

Question Variations

  • "When should you use `@WebMvcTest`?"
  • "What does `@SpringBootTest` load?"
  • "Why avoid full-context tests for every class?"

Why This Is Asked

Testing annotations assess whether candidates choose the smallest realistic Spring context for the behavior under test.

Key Concepts

  • Unit tests should not start Spring unnecessarily.
  • Slice tests target a layer such as MVC or JPA.
  • @SpringBootTest loads the full application context when integration is needed.
  • Tests need isolated deterministic dependencies and configuration.

Question Variations

  • “When should you use @WebMvcTest?”
  • “What does @SpringBootTest load?”
  • “Why avoid full-context tests for every class?”

Answers by Technology

+ Add Variant
Spring BootImprove this answer ✏️

Expected Answer (Spring Boot 4.1.0 / Java 17+)

Choose the smallest test scope that provides confidence. Plain unit tests are fastest for domain logic. Use @WebMvcTest for controller and MVC behavior with mocked collaborators, @DataJpaTest for persistence behavior, and @SpringBootTest only when the full application context or cross-layer integration is needed. Keep external services isolated and test error, authorization, and validation paths as well as success cases.

Why It Matters

Focused tests give fast feedback, while a smaller number of full-context tests catches real wiring defects without making the suite slow and brittle.

Code Example

@WebMvcTest(UserController.class)
class UserControllerTest {
  @Autowired MockMvc mvc;
  @Test void rejectsInvalidBody() throws Exception {
    mvc.perform(post("/users").contentType(APPLICATION_JSON).content("{}"))
      .andExpect(status().isBadRequest());
  }
}

Common Mistakes

  • Using @SpringBootTest for every test: The suite is slow and configuration-heavy.
  • Testing only controllers without response assertions: HTTP status and validation contracts regress unnoticed.

Follow-up Questions

  • What does @WebMvcTest target? (Answer: The MVC slice around selected controllers.)
  • When use a full context? (Answer: When integration wiring itself is under test.)