spring-boot-testing

Selects Spring Boot test slices, Testcontainers, Mockito, and AssertJ techniques for integration and slice tests.

Updated Sep 10, 2026
One-click install
npx skills add https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula --skill spring-boot-testing-serpro-workshop-fortaleza
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: spring-boot-testing
Source: https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula/tree/main/.github/skills/spring-boot-testing
Command: npx skills add https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula --skill spring-boot-testing-serpro-workshop-fortaleza

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Choosing the right Spring Boot testing technique is confusing: developers often overuse @SpringBootTest, test against H2 instead of real databases, or mix up deprecated and current APIs. This Skill maps each testing scenario to the correct slice annotation and tooling for a Spring Boot 3.3 + JUnit 5 stack. ## Core Features & Use Cases - Test slice selection: Decision matrix and tree for @WebMvcTest, @DataJpaTest, @RestClientTest, @JsonTest, and @SpringBootTest, so you always use the narrowest slice that gives confidence. - Real-database testing: Testcontainers setup for running @DataJpaTest and integration tests against PostgreSQL 16 instead of H2. - Assertion and mocking guidance: AssertJ fluent assertions for scalars and collections, Mockito patterns with @MockBean, and Instancio for generating complex test fixtures. - Version boundary clarity: Clearly marks Spring Boot 3.4+/4.0 APIs (MockMvcTester, @MockitoBean, RestTestClient) as out of scope, with a migration reference for future upgrades. - Use Case: You need to test a JPA repository query. The Skill directs you to @DataJpaTest with a PostgreSQL Testcontainers container, TestEntityManager for data setup, and AssertJ assertions, producing a ready-to-adapt test template. ## Quick Start Ask the assistant which Spring Boot test slice to use for your controller, repository, or REST client scenario and request a complete test example.

Frequently Asked Questions about spring-boot-testing

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

FAQPage Schema
How do I choose between @WebMvcTest, @DataJpaTest, and @SpringBootTest?

Use @WebMvcTest for controller and HTTP semantics, @DataJpaTest for repository queries and JPA mappings, and @SpringBootTest only for full application integration. The rule is to pick the narrowest slice that still gives confidence, keeping the suite fast.

How do I test a Spring Data JPA repository against a real PostgreSQL database?

Annotate the test with @DataJpaTest, @Testcontainers, and @AutoConfigureTestDatabase(replace = NONE), then declare a static PostgreSQLContainer with @Container and @ServiceConnection. Use TestEntityManager to persist fixtures and AssertJ for assertions.

Should I use MockMvc or MockMvcTester in Spring Boot 3.3?

In Spring Boot 3.3 use classic MockMvc with perform() and andExpect(), since MockMvcTester requires Spring Boot 3.4 or later. The same applies to @MockBean versus @MockitoBean, which only arrives in 3.4.

Why should I avoid H2 for Spring Boot repository tests?

H2 can hide database-specific behavior such as JSONB columns, array types, or dialect-specific SQL, reducing parity with production. Testcontainers runs a real PostgreSQL 16 container, giving accurate results for @DataJpaTest and integration tests.

How do I test a REST client that calls an external API in Spring Boot?

Use @RestClientTest targeting your client service, which auto-configures MockRestServiceServer. Program expected requests and stubbed responses with server.expect(requestTo(...)).andRespond(withSuccess().body(...)), then call server.verify() at the end.

When should I use Instancio for test data generation?

Use Instancio when entities or DTOs have three or more properties, to avoid repetitive builder or setter calls. Set only the fields relevant to the test with Instancio.of(Type.class).set(field(...), value).create() and let it fill the rest.