java-springboot-testing
Skill4 starsChanged 40 days agoMITView on GitHub
Expert Spring Boot testing specialist that selects the best testing techniques for your situation with JUnit and AssertJ. Trigger: When writing Spring Boot tests, need testing patterns, or working with test slices.
In this repository18 other files it gives its agents.
- api-designSkill
- changelog-maintenanceSkill
- code-mentorSkill
- create-readmeSkill
- database-designSkill
- dockerSkill
- frontend-design-anti-slopSkill
- frontend-engineeringSkill
- git-commitSkill
- github-essentialsSkill
- java-junitSkill
- java-springbootSkill
- latexSkill
- solution-designSkill
- ui-componentsSkill
- update-readmeSkill
- vault-tech-noteSkill
- web-mvcSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
13 rules. About 0 are common, 11 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 47
Right Tool: Use the narrowest slice that gives you confidence
- Distinctive: line 49
Modern APIs: Prefer MockMvcTester and RestTestClient over legacy alternatives
- Distinctive: line 138
When a method or class is too complex to test effectively:
- Distinctive: line 140
Analyze complexity: - If you need more than 5-7 test cases to cover a single method, it's likely too complex
- Distinctive: line 142
User decision: - If the user agrees to refactor, help identify extraction points
- Distinctive: line 143
Proceed if needed: - If the user decides to continue with the complex code, implement tests despite the difficulty
- Distinctive: line 167
Create helper methods for commonly used objects and mock setup to enhance readability and maintainability.
- Distinctive: line 171
Use descriptive display names to clarify test intent:
- Distinctive: line 185
Always structure tests in this order:
- Distinctive: line 193
Write tests with real production scenarios in mind.
- Distinctive: line 199
Use Jacoco maven plugin for coverage reporting and tracking.
- Very specific: line 247
Resolve the libraryId: - Use context7_resolve-library-id:
- Very specific: line 251
Query the docs: - Use context7_query-docs:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
- Writing tests for Spring Boot applications
- Need help choosing the right test slice
- Setting up test infrastructure
- Working with MockMvc, TestRestTemplate, or RestTestClient
- Writing integration tests with Testcontainers
Instructions
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 configCore 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 |
| Endpoint security, pagination contracts, engine matrix | Tier 1/Tier 2 patterns | references/endpoint-testing.md |
| Repository + JPA queries | @DataJpaTest | references/datajpatest.md |
| Repository tests on other engines / SQLite | engine variety | references/database-testing.md |
| Database state between tests, Flyway migration testing | persistence integrity | references/database-testing.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)
Database State & Migrations
- references/database-testing.md - Data reset strategies, Flyway migration testing discipline, SQLite variant, multi-engine suites, Podman
Performance & Migration
- references/context-caching.md - Speed up test suites
- references/sb4-migration.md - Spring Boot 4.0 changes
Endpoint Patterns
- references/endpoint-testing.md - Security/auth (real chain + jwt()), pagination HTTP contracts, multi-engine prioritization
Commands
# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=OrderControllerTest
# Run tests with specific tag
mvn test -Dgroups=fast
# Run tests with Gradle
gradle test
# Run specific test with Gradle
gradle test --tests OrderControllerTest
# Run integration tests
mvn verify -P integration-testSpring 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>Resources
- Spring Boot Testing Documentation
- JUnit 5 User Guide
- AssertJ Documentation
- Testcontainers Documentation
- Mockito Documentation
Context7 Integration
For up-to-date information on testing with Spring Boot:
- Resolve the libraryId - Use
context7_resolve-library-id:libraryName: "junit 5", "assertj", "mockito", or "spring boot test"query: what you need (e.g., "mocking beans", "assertions best practices")
- Query the docs - Use
context7_query-docs:libraryId: "/junit/junit5", "/assertj/assertj", or "/spring/spring-boot"query: your specific question about testing
Before writing tests, consult Context7 to get updated examples of the library you're going to use.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

