56 results available
Sign in
Skills directory

spring-boot-testing

github

·

Last changed
Not yet hardened

This version isn't hardened by Chainguard

No hardening report available.


SKILL.md

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.xSpring 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?

ScenarioAnnotationReference

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

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. Model those scenarios with synthetic data, such as Instancio or fake values, and keep real customer data, production endpoints and credentials out of tests and test configuration; use Testcontainers or placeholders instead.

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 as a target, not an enforced minimum unless the user asks for one
  • 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)

In Boot 4, add the technology test starter for each slice you use, as listed in the compatibility table above. Omit the version only for artifacts managed by the Spring Boot parent or BOM. For unmanaged ones, such as the Jacoco maven plugin or Instancio, pin an explicit released version and check the exact group and artifact names against the official coordinates.

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>

Install

Need this version hardened?


View source

Harden your own skills

Bring Chainguard's hardening pipeline to your first-party skills — custom rulesets, automated remediation, full audit trail.

Provenance

Upstream sourcehttps://github.com/github/awesome-copilot
Upstream commitd7652c694eb2bf44e4ee2aba81f358951672f31c
OCI reference

skills.cgr.dev/public/github.com/github/skills/spring-boot-testing:latest

License

MIT

Files

HARDENING.md

LICENSE

SKILL.md

references/assertj-basics.md

references/assertj-collections.md

references/context-caching.md

references/datajpatest.md

references/failsafe-integration-tests.md

references/instancio.md

references/mockitobean.md

references/mockmvc-classic.md

references/mockmvc-tester.md

references/restclienttest.md

references/resttestclient.md

references/sb4-migration.md

references/test-slices-overview.md

references/testcontainers-jdbc.md

references/webmvctest.md


The trusted source for open source

Talk to an expert
PrivacyTerms

Product

Chainguard ContainersChainguard LibrariesChainguard VMsChainguard OS PackagesChainguard ActionsChainguard Agent SkillsIntegrationsPricing
© 2026 Chainguard, Inc. All Rights Reserved.
Chainguard® and the Chainguard logo are registered trademarks of Chainguard, Inc. in the United States and/or other countries.
The other respective trademarks mentioned on this page are owned by the respective companies and use of them does not imply any affiliation or endorsement.