spring-boot-testing
github/awesome-copilot
Expert Spring Boot 4 testing specialist selecting optimal techniques with JUnit 6 and AssertJ.
What is spring-boot-testing?
This skill guides testing Spring Boot 4 applications using modern patterns and best practices. It helps you choose the right test slice (@WebMvcTest, @DataJpaTest, @RestClientTest, @JsonTest, @SpringBootTest) and assertion style for your scenario, following the test pyramid principle and leveraging tools like MockMvcTester, RestTestClient, and Testcontainers.
- Select optimal test slice based on what you're testing (controller, repository, REST client, JSON, or full app)
- Write fluent AssertJ-style assertions instead of verbose matchers
- Apply test pyramid strategy: unit tests (fast) > slice tests (focused) > integration tests (complete)
- Use modern Spring Boot 4 APIs like MockMvcTester, RestTestClient, and @MockitoBean
- Implement Testcontainers for real database testing in @DataJpaTest scenarios
- Generate complex test objects with Instancio for 3+ property scenarios
How to install spring-boot-testing
npx skills add https://github.com/github/awesome-copilot --skill spring-boot-testing- Spring Boot 4 project with Maven or Gradle
- JUnit 6 and AssertJ in test dependencies
- spring-boot-starter-test dependency configured
- Docker installed if using Testcontainers for database testing
How to use spring-boot-testing
- 1.Identify what layer you're testing: controller, repository, service, or REST client
- 2.Select the appropriate test slice annotation from the decision tree (@WebMvcTest, @DataJpaTest, @RestClientTest, @JsonTest, or @SpringBootTest)
- 3.Use MockMvcTester for web layer tests or RestTestClient for REST client tests instead of legacy alternatives
- 4.Write assertions using AssertJ fluent style for readability
- 5.For database tests, configure Testcontainers to use real databases instead of in-memory H2
- 6.Organize tests in order: main scenario (happy path), alternative paths, then error cases
- 7.Aim for 80% code coverage focusing on business-critical paths, complex algorithms, and error handling
- 8.Use @DisplayName annotations to clarify test intent with descriptive names
Use cases
- Testing Spring Boot controller endpoints with @WebMvcTest and MockMvcTester
- Testing JPA repository queries against real databases using @DataJpaTest with Testcontainers
- Testing REST client code that calls external APIs with @RestClientTest
- Testing JSON serialization/deserialization with @JsonTest
- Full integration tests with @SpringBootTest when testing complete application workflows
- Spring Boot developers writing unit and integration tests
- QA engineers automating Spring Boot application testing
- Teams migrating to Spring Boot 4 and modernizing test suites
- Developers seeking to improve test coverage and code quality
spring-boot-testing FAQ
Use @WebMvcTest with MockMvcTester (Spring Boot 3.2+) for testing controller endpoints with HTTP semantics. This loads only the web layer without the full application context, making tests fast and focused.
Use @SpringBootTest only when you need to test the full application with all components loaded. For most cases, use narrower slices (@WebMvcTest, @DataJpaTest, @RestClientTest, @JsonTest) that are faster and more focused.
Use @DataJpaTest with Testcontainers to test against a real database (PostgreSQL, MySQL, etc.) instead of in-memory H2. This ensures your JPA queries work correctly in production.
MockMvcTester (Spring Boot 3.2+) provides AssertJ-style fluent assertions and is the modern approach. Traditional MockMvc uses verbose matchers. Prefer MockMvcTester for new tests.
Order tests as: (1) main scenario (happy path), (2) alternative valid scenarios and edge cases, (3) error conditions and exceptions. Use @DisplayName for descriptive test names and aim for 80% code coverage on business-critical paths.
Full instructions (SKILL.md)
Source of truth, from github/awesome-copilot.
name: spring-boot-testing description: Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.
Spring Boot Testing
This skill provides expert guide for testing Spring Boot 4 applications with modern patterns and best practices.
Core Principles
- Test Pyramid: Unit (fast) > Slice (focused) > Integration (complete)
- Right Tool: Use the narrowest slice that gives you confidence
- AssertJ Style: Fluent, readable assertions over verbose matchers
- Modern APIs: Prefer MockMvcTester and RestTestClient over legacy alternatives
Which Test Slice?
| Scenario | Annotation | Reference |
|---|---|---|
| Controller + HTTP semantics | @WebMvcTest | references/webmvctest.md |
| Repository + JPA queries | @DataJpaTest | references/datajpatest.md |
| REST client + external APIs | @RestClientTest | references/restclienttest.md |
| JSON (de)serialization | @JsonTest | references/test-slices-overview.md |
| Full application | @SpringBootTest | references/test-slices-overview.md |
Test Slices Reference
- references/test-slices-overview.md - Decision matrix and comparison
- references/webmvctest.md - Web layer with MockMvc
- references/datajpatest.md - Data layer with Testcontainers
- references/restclienttest.md - REST client testing
Testing Tools Reference
- references/mockmvc-tester.md - AssertJ-style MockMvc (3.2+)
- references/mockmvc-classic.md - Traditional MockMvc (pre-3.2)
- references/resttestclient.md - Spring Boot 4+ REST client
- references/mockitobean.md - Mocking dependencies
Assertion Libraries
- references/assertj-basics.md - Scalars, strings, booleans, dates
- references/assertj-collections.md - Lists, Sets, Maps, arrays
Testcontainers
- references/testcontainers-jdbc.md - PostgreSQL, MySQL, etc.
Test Data Generation
- references/instancio.md - Generate complex test objects (3+ properties)
Performance & Migration
- references/context-caching.md - Speed up test suites
- references/sb4-migration.md - Spring Boot 4.0 changes
Quick Decision Tree
Testing a controller endpoint?
Yes → @WebMvcTest with MockMvcTester
Testing repository queries?
Yes → @DataJpaTest with Testcontainers (real DB)
Testing business logic in service?
Yes → Plain JUnit + Mockito (no Spring context)
Testing external API client?
Yes → @RestClientTest with MockRestServiceServer
Testing JSON mapping?
Yes → @JsonTest
Need full integration test?
Yes → @SpringBootTest with minimal context config
Spring Boot 4 Highlights
- RestTestClient: Modern alternative to TestRestTemplate
- @MockitoBean: Replaces @MockBean (deprecated)
- MockMvcTester: AssertJ-style assertions for web tests
- Modular starters: Technology-specific test starters
- Context pausing: Automatic pausing of cached contexts (Spring Framework 7)
Testing Best Practices
Code Complexity Assessment
When a method or class is too complex to test effectively:
- Analyze complexity - If you need more than 5-7 test cases to cover a single method, it's likely too complex
- Recommend refactoring - Suggest breaking the code into smaller, focused functions
- User decision - If the user agrees to refactor, help identify extraction points
- Proceed if needed - If the user decides to continue with the complex code, implement tests despite the difficulty
Example of refactoring recommendation:
// Before: Complex method hard to test
public Order processOrder(OrderRequest request) {
// Validation, discount calculation, payment, inventory, notification...
// 50+ lines of mixed concerns
}
// After: Refactored into testable units
public Order processOrder(OrderRequest request) {
validateOrder(request);
var order = createOrder(request);
applyDiscount(order);
processPayment(order);
updateInventory(order);
sendNotification(order);
return order;
}
Avoid Code Redundancy
Create helper methods for commonly used objects and mock setup to enhance readability and maintainability.
Test Organization with @DisplayName
Use descriptive display names to clarify test intent:
@Test
@DisplayName("Should calculate discount for VIP customer")
void shouldCalculateDiscountForVip() { }
@Test
@DisplayName("Should reject order when customer has insufficient credit")
void shouldRejectOrderForInsufficientCredit() { }
Test Coverage Order
Always structure tests in this order:
- Main scenario - The happy path, most common use case
- Other paths - Alternative valid scenarios, edge cases
- Exceptions/Errors - Invalid inputs, error conditions, failure modes
Test Production Scenarios
Write tests with real production scenarios in mind. This makes tests more relatable and helps understand code behavior in actual production cases.
Test Coverage Goals
Aim for 80% code coverage as a practical balance between quality and effort. Higher coverage is beneficial but not the only goal.
Use Jacoco maven plugin for coverage reporting and tracking.
Coverage Rules:
- 80+% coverage minimum
- Focus on meaningful assertions, not just execution
What to Prioritize:
- Business-critical paths (payment processing, order validation)
- Complex algorithms (pricing, discount calculations)
- Error handling (exceptions, edge cases)
- Integration points (external APIs, databases)
Dependencies (Spring Boot 4)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For WebMvc tests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
Related skills
More from github/awesome-copilot and the wider catalog.

sql-code-review
Comprehensive SQL code review for security, performance, and maintainability across MySQL, PostgreSQL, SQL Server, and Oracle.

sql-optimization
Universal SQL performance optimization across MySQL, PostgreSQL, SQL Server, and Oracle with execution plan analysis and indexing strategies.

structured-autonomy-generate
Turns a PR plan.md into a complete, copy-paste-ready implementation.md with full code and verification checklists.

structured-autonomy-implement
Executes a given implementation plan exactly, step by step, with no scope creep.

structured-autonomy-plan
Turns feature requests into a researched, commit-by-commit implementation plan—no code written.

suggest-awesome-github-copilot-agents
Finds and recommends GitHub Copilot Custom Agents from awesome-copilot, flagging duplicates and outdated versions.