Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springhard

`@WebMvcTest` vs `@SpringBootTest` — when to use which?

Tags
#testing#webmvctest#springboottest
Back to categoryPractice quiz

Answer

`@WebMvcTest` is a slice test: it loads MVC components (controllers, filters) and is fast, usually with mocked dependencies. `@SpringBootTest` loads the full application context and is better for integration tests, but slower.

Advanced answer

Deep dive

These two annotations trade realism for speed.

`@WebMvcTest`

  • Loads a **slice** of the context: MVC infrastructure + selected controller(s).
  • Great for testing routing, validation, serialization, status codes, and error mapping.
  • Typically uses `MockMvc` and mocks service dependencies (`@MockBean`).

`@SpringBootTest`

  • Boots the **full** application context.
  • Best for integration tests across layers (security, persistence, messaging).
  • Often paired with Testcontainers to run real dependencies.

Practical guidance

  • Use `@WebMvcTest` for controller-focused tests.
  • Use `@SpringBootTest` for end-to-end integration scenarios.
  • Consider other test slices: `@DataJpaTest`, `@JsonTest`, `@WebFluxTest`.

Common pitfalls

  • Using `@SpringBootTest` everywhere (slow suite, brittle tests).
  • Over-mocking in `@WebMvcTest` and missing real serialization/security issues.
  • Not importing/configuring security for slice tests (or disabling filters without realizing the coverage gap).

Related questions

Testing
What is property-based testing and when would you use it?
#property-based#invariants#testing
DevOps
Describe the typical stages of a CI pipeline and common failure points.
#ci#pipeline#build
Monoliths
How do you prevent performance regressions in a large monolith?
#monolith
#performance
#observability
Monoliths
How do you approach integration testing in a monolith without making CI too slow?
#testing#monolith#ci
Microservices
Consumer-driven contract tests: what are they and why use them?
#microservices#testing#contracts
Monoliths
How do you refactor a messy monolith without stopping feature delivery?
#refactoring#incremental#testing