TestForge | Aidevops | 📊 Plogger ✍️ Blog 📚 Docs
plogger

AI DevOps Korea

Turn AI service development and operations into one improvement loop

Aidevops.kr covers LLMOps, RAG, agents, observability, evaluation, and cost-performance optimization for production AI services.

Spring Boot Test Slices: @WebMvcTest and @DataJpaTest

· Updated Apr 23
Spring Boot Test Slices: @WebMvcTest and @DataJpaTest diagram
Visual guide to the key flow, architecture, and decision points covered in this post.
Spring Boot test slices are not just a speed trick. They are a way to reduce context scope intentionally so a test proves one boundary clearly and cheaply.

That makes slices valuable only when the team knows exactly what each slice is supposed to prove.

Test slices exist to narrow proof targets

The core idea is simple:

  • load less application context
  • focus on one technical boundary
  • get faster, clearer feedback

This helps prevent a common failure mode where every test becomes a full @SpringBootTest and the suite gets slow without becoming more informative.

@WebMvcTest is for HTTP contract behavior

@WebMvcTest is strongest when the goal is to verify:

  • request mapping
  • validation behavior
  • status codes
  • JSON response shape
  • exception-to-response translation
  • security behavior at the MVC layer

It is not meant to prove repository behavior or full service integration. Dependencies outside the slice should be provided deliberately through mocks or targeted test configuration.

@DataJpaTest is for persistence reality

@DataJpaTest is useful for proving:

  • repository query correctness
  • JPA mappings
  • generated SQL behavior
  • pagination and fetch behavior
  • transaction interaction in the persistence layer

This is where teams should catch query mistakes, mapping surprises, and database-specific issues before they leak upward.

Other slices matter too

Depending on the codebase, focused slices like these can be valuable:

  • @JsonTest for serialization and deserialization contracts
  • @RestClientTest for HTTP client boundaries
  • messaging or other specialized slices where supported

The underlying principle is always the same: keep the test focused on one boundary of proof.

Slices work best when application boundaries are already clear

A slice becomes awkward when the production design itself is overly tangled.

Common signs of trouble:

  • controllers doing too much business logic
  • services tightly coupled to unrelated infrastructure
  • repositories carrying domain decisions

When boundaries are muddy in production code, slice tests often become mock-heavy and confusing. That is a design signal, not just a test problem.

@SpringBootTest still has an important role

Avoiding full-context tests entirely is not the goal. @SpringBootTest is useful when the team needs to prove:

  • real cross-layer wiring
  • configuration-sensitive behavior
  • important integration paths
  • a small number of high-value end-to-end application flows

The mistake is not using @SpringBootTest. The mistake is using it for every proof target.

Common anti-patterns

Watch for these patterns:

  • using @SpringBootTest for everything
  • dragging unrelated dependencies into a slice test
  • proving the same defect class redundantly in slice and full-context tests
  • writing slice tests without a clear statement of what boundary is being verified

These patterns make the suite slower without making it clearer.

Review checklist

Before calling slice usage healthy, ask:

  • What exact boundary is this test proving?
  • Is this the cheapest realistic layer to prove it?
  • Are dependencies outside the slice controlled intentionally?
  • Would a full-context test add unique information here?
  • Is the application design clear enough that the slice stays readable?

Closing judgment

Spring test slices are not just a speed hack. They are a responsibility-separation tool. The clearer the proof target, the healthier the overall test strategy becomes.

Continue Reading

Related posts

Next Path

Keep exploring this topic as a system