Index of every page on this site: llms.txt
All skills
github avatar

/spring-boot-testing

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

Expert Spring Boot testing specialist that selects the best testing techniques for your situation: test slices, Testcontainers with @ServiceConnection, Failsafe integration tests, MockitoBean, JUnit and AssertJ. Written for Spring Boot 4 with notes for Boot 3.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/spring-boot-testing

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

SKILL.md

≈70 tokens for metadata: the name and description. ≈2k when used: this file. ≈18k more on demand in 15 files.

Description uses 3.5% of example budget

Before choosing a Skill, your Agent reads its name and description. All available Skills share that space.

  • A shorter description leaves more room for other Skills. This entry exceeds our 1% size suggestion.

In our Claude Code example, all Skill names and descriptions share 8,000 characters. This Skill uses ≈282 characters, or 3.5%.

The 1% threshold is a size suggestion. Longer descriptions can still fit.

Your model, settings, and other Skills decide how much text your Agent can read.

Example settings and source

The example uses a 200k-token context and default Claude Code settings. The count includes the name, description, separators, and when_to_use when present. Codex also counts local file paths.

Skit's source and limits: Codex 0.160.1, Claude Code 2.1.292.

Spring Boot Testing

This skill provides expert guidance for testing Spring Boot applications with modern patterns and best practices. Examples target Spring Boot 4; see the compatibility note below for Boot 3.

Compatibility: detect first

Read the project's Spring Boot version and test dependencies before writing tests, and do not upgrade them implicitly.

Spring Boot 3.x Spring Boot 4.x
JUnit 5 (Jupiter) 6 by default (same org.junit.jupiter.api annotations; JUnit 5 tests keep working)
Mock beans @MockitoBean / @MockitoSpyBean from 3.4 (Spring Framework 6.2); @MockBean / @SpyBean are deprecated @MockitoBean replaces @MockBean (do not use the old ones)
Test starters spring-boot-starter-test covers everything modular: add spring-boot-starter-webmvc-test, spring-boot-starter-data-jpa-test, ...
Slice imports org.springframework.boot.test.autoconfigure... moved, e.g. org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest
Testcontainers 1.x names 2.x names (testcontainers-postgresql)

Details: references/sb4-migration.md, references/mockitobean.md.

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 and integration tests

Maven gotcha: Failsafe declared only in <pluginManagement> means *IT tests never run. Declare it in <build><plugins> with the integration-test and verify goals and run ./mvnw verify.

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)

See also: https://docs.spring.io/spring-boot/reference/testing/spring-boot-applications.html and https://docs.spring.io/spring-boot/reference/features/dev-services.html.

<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>

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts18d4 checks · Risk SAFE
  • Gen Agent Trust Hub18d

    This skill is a comprehensive educational resource for Spring Boot testing. It provides best practices and code templates for modern Java testing frameworks. No malicious patterns, vulnerabilities, or security risks were identified during the analysis.

  • Socket18d

    No alerts

  • Snyk18d

    Risk: LOW · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated last month
  • Testing
  • spring-boot
  • junit
  • assertj
  • mockmvc
  • testcontainers
  • java
  • mockito
  • integration-testing
  • webmvctest

README badge

README badge for github/awesome-copilot/spring-boot-testing

Guides Spring Boot 4 testing with test slices (@WebMvcTest, @DataJpaTest, @RestClientTest), modern assertion libraries (AssertJ, MockMvcTester), and practical patterns like Testcontainers for database integration and context caching for performance. Covers the test pyramid, complexity assessment, and 80% coverage goals.

Generated from the current SKILL.md.

Does this skill work with Spring Boot 3, or is it Spring Boot 4 only?
This skill is written for Spring Boot 4. It covers Spring Boot 4-specific features like RestTestClient, @MockitoBean, and MockMvcTester. Some patterns (like traditional MockMvc) have backward-compatible alternatives documented, but the primary guidance assumes Spring Boot 4.
What assertion library does this skill recommend?
AssertJ. The skill explicitly prefers AssertJ's fluent, readable assertion style over verbose matchers, and includes references for assertions on scalars, collections, and other data types.
Does this cover testing with real databases, or only mocks?
Both. The skill recommends Testcontainers for data layer tests (@DataJpaTest) to use real databases in tests, and includes references for PostgreSQL, MySQL, and other JDBC sources.
What's the recommended test coverage target?
80% as a practical balance between quality and effort. The skill prioritizes business-critical paths, complex algorithms, error handling, and integration points over achieving maximum coverage.
Does this skill help with test data generation?
Yes. It references Instancio for generating complex test objects with 3+ properties, reducing boilerplate in test setup.

Generated from the current SKILL.md. These answers refresh after source changes.