Catalog
github/spring-boot-testing

github

spring-boot-testing

Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.

global
New~1.7k
v1.0Saved Jun 26, 2026

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?

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

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>
Files15
15 files · 67.1 KB

Select a file to preview

Overall Score

89/100

Grade

A

Excellent

Safety

92

Quality

88

Clarity

89

Completeness

84

Summary

This skill is a comprehensive reference guide for testing Spring Boot 4 applications with modern patterns and best practices. It provides structured guidance on selecting the right test slice (unit, slice, integration), using modern APIs like MockMvcTester and RestTestClient, and following a test pyramid approach with JUnit 6 and AssertJ.

Detected Capabilities

code analysisdocumentation readingtest guidancecode pattern recommendationdependency analysis

Trigger Keywords

Phrases that MCP clients use to match this skill to user intent.

test spring bootjunit testingmockito mockingtestcontainerscontroller testingrepository testingrest api testingassertj assertions

Referenced Domains

External domains referenced in skill content, detected by static analysis.

api.example.comapi.weather.comwww.instancio.org

Use Cases

  • Write Spring Boot controller tests with MockMvcTester and AssertJ assertions
  • Test JPA repositories with Testcontainers for real database parity
  • Choose the right test slice (@WebMvcTest, @DataJpaTest, @RestClientTest, @SpringBootTest) for your scenario
  • Migrate from Spring Boot 3.x to 4.0 testing patterns (@MockBean to @MockitoBean)
  • Test REST clients calling external APIs with MockRestServiceServer
  • Generate complex test data with Instancio for 3+ property objects
  • Optimize test suite performance through context caching strategies

Quality Notes

  • Comprehensive reference with 14 well-organized supporting files covering all major testing patterns
  • Clear decision trees and matrices for selecting appropriate test slices and assertions
  • Practical code examples in every reference file with real Order/Product domain scenarios
  • Strong coverage of Spring Boot 4 migration patterns with before/after comparisons
  • Excellent use of Java 25 features (records, text blocks, pattern matching, sequenced collections) in examples
  • Well-structured progression from simple (unit) to complex (integration) tests
  • Each reference file is focused and self-contained while cross-linking to related topics
  • Best practices sections in nearly every file with actionable guidance
  • Includes common mistakes and migration paths from older patterns
  • Dependencies clearly specified for each test slice and Spring Boot version
Model: claude-haiku-4-5-20251001Analyzed: Jun 26, 2026

Reviews

Add this skill to your library to leave a review.

No reviews yet

Be the first to share your experience.

Add github/spring-boot-testing to your library

Command Palette

Search for a command to run...