spring-boot-testing

Guides selection and implementation of Spring Boot 4 test slices with JUnit 6 and AssertJ.

1|1|Updated Jan 29, 2026
One-click install
npx skills add https://github.com/ultraviollettnympho/transit-ticket --skill spring-boot-testing-ultraviollettnympho
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: spring-boot-testing
Source: https://github.com/ultraviollettnympho/transit-ticket/tree/main/.github/skills/spring-boot-testing
Command: npx skills add https://github.com/ultraviollettnympho/transit-ticket --skill spring-boot-testing-ultraviollettnympho

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Choosing the right testing approach for Spring Boot applications is confusing: developers often overuse @SpringBootTest, rely on deprecated APIs like @MockBean and TestRestTemplate, or write verbose assertions that are hard to maintain. This Skill provides expert guidance on selecting the narrowest appropriate test slice and writing modern, readable tests for Spring Boot 4. ## Core Features & Use Cases - Test Slice Selection: Decision matrices and a quick decision tree for choosing between @WebMvcTest, @DataJpaTest, @RestClientTest, @JsonTest, and @SpringBootTest. - Modern Testing APIs: Guidance on MockMvcTester, RestTestClient, @MockitoBean, and Testcontainers with real databases instead of H2. - AssertJ & Test Data: Fluent assertion patterns for scalars and collections, plus Instancio for generating complex test objects. - Use Case: You need to test a REST controller in a Spring Boot 4 project. The Skill directs you to @WebMvcTest with MockMvcTester, shows the object-conversion assertion pattern, and provides the correct modular starter dependency. ## Quick Start Ask the assistant to write a test for your Spring Boot controller or repository and it will select the right test slice with modern Spring Boot 4 patterns.

Frequently Asked Questions about spring-boot-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test a Spring Boot controller with MockMvc?

Use @WebMvcTest with MockMvcTester on Spring Boot 3.2+ for AssertJ-style assertions. Mock service dependencies with @MockitoBean, then convert JSON responses to typed objects with bodyJson().convertTo() for type-safe assertions.

What is the difference between @WebMvcTest and @SpringBootTest?

@WebMvcTest loads only the web layer (controllers, Jackson, security filters) and runs fast, while @SpringBootTest loads the entire application context for full integration testing. Prefer the narrowest slice that gives you confidence.

How do I replace @MockBean in Spring Boot 4?

Replace @MockBean with @MockitoBean and @SpyBean with @MockitoSpyBean, as the older annotations are deprecated in Spring Boot 4. The new annotations also support mocking prototype-scoped beans via Spring Framework 7.

Should I use H2 or Testcontainers for repository tests?

Testcontainers with a real database like PostgreSQL is recommended over H2 for production parity, since H2 can miss database-specific issues. Combine @DataJpaTest with @ServiceConnection and a PostgreSQLContainer for accurate results.

Why is my Spring Boot test suite slow?

Slow suites usually come from overusing @SpringBootTest or creating many distinct context configurations. Group tests by configuration, minimize @TestPropertySource variations, avoid @DirtiesContext, and use narrow test slices to improve context cache hits.

When should I use Instancio for test data?

Use Instancio when objects have three or more properties to avoid repetitive builder or setter calls. It generates fully populated objects with random data while letting you set only the fields relevant to your test scenario.