PluginBench
Skill
Official
Pass
Audit score 90

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
Prerequisites
  • 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
Claude Code
Cursor
Windsurf
Cline

How to use spring-boot-testing

  1. 1.Identify what layer you're testing: controller, repository, service, or REST client
  2. 2.Select the appropriate test slice annotation from the decision tree (@WebMvcTest, @DataJpaTest, @RestClientTest, @JsonTest, or @SpringBootTest)
  3. 3.Use MockMvcTester for web layer tests or RestTestClient for REST client tests instead of legacy alternatives
  4. 4.Write assertions using AssertJ fluent style for readability
  5. 5.For database tests, configure Testcontainers to use real databases instead of in-memory H2
  6. 6.Organize tests in order: main scenario (happy path), alternative paths, then error cases
  7. 7.Aim for 80% code coverage focusing on business-critical paths, complex algorithms, and error handling
  8. 8.Use @DisplayName annotations to clarify test intent with descriptive names

Use cases

Good for
  • 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
Who it's for
  • 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

Which test slice should I use for testing a controller endpoint?

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.

When should I use @SpringBootTest vs. a narrower test slice?

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.

How do I test database queries in Spring Boot?

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.

What's the difference between MockMvcTester and traditional MockMvc?

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.

How should I structure my test cases?

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

  1. Test Pyramid: Unit (fast) > Slice (focused) > Integration (complete)
  2. Right Tool: Use the narrowest slice that gives you confidence
  3. AssertJ Style: Fluent, readable assertions over verbose matchers
  4. Modern APIs: Prefer MockMvcTester and RestTestClient over legacy alternatives

Which Test Slice?

ScenarioAnnotationReference
Controller + HTTP semantics@WebMvcTestreferences/webmvctest.md
Repository + JPA queries@DataJpaTestreferences/datajpatest.md
REST client + external APIs@RestClientTestreferences/restclienttest.md
JSON (de)serialization@JsonTestreferences/test-slices-overview.md
Full application@SpringBootTestreferences/test-slices-overview.md

Test Slices Reference

Testing Tools Reference

Assertion Libraries

Testcontainers

Test Data Generation

Performance & Migration

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:

  1. Analyze complexity - If you need more than 5-7 test cases to cover a single method, it's likely too complex
  2. Recommend refactoring - Suggest breaking the code into smaller, focused functions
  3. User decision - If the user agrees to refactor, help identify extraction points
  4. 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:

  1. Main scenario - The happy path, most common use case
  2. Other paths - Alternative valid scenarios, edge cases
  3. 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:

  1. Business-critical paths (payment processing, order validation)
  2. Complex algorithms (pricing, discount calculations)
  3. Error handling (exceptions, edge cases)
  4. 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>