Intermediate Reading time: ~3 min

Spring Testing

@SpringBootTest, slice tests, @WebMvcTest, @DataJpaTest

Spring Testing

Spring testing is about loading the right amount of context, not automatically the largest one.

1. Definition

@SpringBootTest gives an application-near test, while slice annotations such as @WebMvcTest or @DataJpaTest load only the relevant layer. That gives faster and more focused feedback.

2. Core Concepts

What happens underneath

@SpringBootTest gives an application-near test, while slice annotations such as @WebMvcTest or @DataJpaTest load only the relevant layer. That gives faster and more focused feedback.

When it is useful

Spring testing is about loading the right amount of context, not automatically the largest one. In interviews, highlight common use cases and the related Spring annotations.

What interviewers ask

Common questions cover Spring Testing trade-offs, debugging, and production pitfalls.

3. Practical Usage

  • Use Spring Testing when the problem truly calls for it.
  • Look at configuration and annotations together because both shape runtime behavior in Spring.
  • During debugging always inspect startup logs, the bean graph, and active profiles.
  • Prefer small components with clear responsibilities.
  • In interviews, mention the production trade-off, not only the annotation name.

4. Code Examples

The following example demonstrates Spring Testing with real Spring annotations.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(UserController.class)
class UserControllerTest {
    @Autowired
    MockMvc mockMvc;

    @Test
    void shouldReturnOk() throws Exception {
        mockMvc.perform(get("/api/users/1"))
                .andExpect(status().isOk());
    }
}

Slice tests are ideal when you care about one layer rather than the whole application working together.

5. Trade-offs

  • Convention vs explicit configuration — Spring speeds development through convention, but sometimes explicit control is required for predictable behavior.
  • Fast development vs transparency — Less boilerplate is good, but too much framework magic can make debugging harder.
  • Abstraction vs control — Higher abstraction accelerates development, but you still need to understand low-level behavior.

6. Common Mistakes

  • Memorizing annotations without understanding runtime behavior.
  • Changing defaults without understanding the consequences.
  • Keeping oversized components with weak boundaries.
  • Ignoring logs and diagnostic tooling.
  • Not aligning the test strategy with the specific layer.

7. Senior-level Insights

  • A senior Spring engineer also looks at which proxy, bean post-processor, or filter is active under the hood.
  • Most hard bugs are not about annotation names but about lifecycle and ordering.
  • In production thinking, startup time, memory, observability, and rollback strategy all matter.

8. Glossary

  • Spring Testing: A Spring concept or mechanism related to this topic.
  • Bean: An object managed by Spring.
  • Proxy: An intermediate object that can add behavior.
  • Configuration: Properties or bean definitions that shape runtime behavior.
  • Context: The current application container state in Spring.

9. Cheatsheet

  • Spring testing is about loading the right amount of context, not automatically the largest one.
  • Understand the underlying lifecycle.
  • In interviews, mention the trade-off, not just the annotation.
  • For production bugs, start with logs, bean graph, and active profiles.
  • The simplest working solution is often the best one.

🎮 Games

8 questions