---
name: spring-boot-testing
description: '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.'
title: spring-boot-testing
canonical_url: https://skilld.dev/gh/github/awesome-copilot/spring-boot-testing
last_updated: 2026-11-02T02:00:53.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/assertj-basics.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/assertj-basics.md), [references/assertj-collections.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/assertj-collections.md), [references/context-caching.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/context-caching.md), [references/datajpatest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/datajpatest.md), [references/failsafe-integration-tests.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/failsafe-integration-tests.md), [references/instancio.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/instancio.md), [references/mockitobean.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/mockitobean.md), [references/mockmvc-classic.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/mockmvc-classic.md), [references/mockmvc-tester.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/mockmvc-tester.md), [references/restclienttest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/restclienttest.md), [references/resttestclient.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/resttestclient.md), [references/sb4-migration.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/sb4-migration.md), [references/test-slices-overview.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/test-slices-overview.md), [references/testcontainers-jdbc.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/testcontainers-jdbc.md), [references/webmvctest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/webmvctest.md).
>
> If the user asked to install this Skill, run `npx skilld install github/awesome-copilot/spring-boot-testing`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/spring-boot-testing`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/github/awesome-copilot/spring-boot-testing) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/spring-boot-testing`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/spring-boot-testing --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/spring-boot-testing --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# 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](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/sb4-migration.md), [references/mockitobean.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/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](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/webmvctest.md) |
| Repository + JPA queries | `@DataJpaTest` | [references/datajpatest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/datajpatest.md) |
| REST client + external APIs | `@RestClientTest` | [references/restclienttest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/restclienttest.md) |
| JSON (de)serialization | `@JsonTest` | [references/test-slices-overview.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/test-slices-overview.md) |
| Full application | `@SpringBootTest` | [references/test-slices-overview.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/test-slices-overview.md) |

## Test Slices Reference

- [references/test-slices-overview.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/test-slices-overview.md) - Decision matrix and comparison
- [references/webmvctest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/webmvctest.md) - Web layer with MockMvc
- [references/datajpatest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/datajpatest.md) - Data layer with Testcontainers
- [references/restclienttest.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/restclienttest.md) - REST client testing

## Testing Tools Reference

- [references/mockmvc-tester.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/mockmvc-tester.md) - AssertJ-style MockMvc (3.2+)
- [references/mockmvc-classic.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/mockmvc-classic.md) - Traditional MockMvc (pre-3.2)
- [references/resttestclient.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/resttestclient.md) - Spring Boot 4+ REST client
- [references/mockitobean.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/mockitobean.md) - Mocking dependencies

## Assertion Libraries

- [references/assertj-basics.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/assertj-basics.md) - Scalars, strings, booleans, dates
- [references/assertj-collections.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/assertj-collections.md) - Lists, Sets, Maps, arrays

## Testcontainers and integration tests

- [references/testcontainers-jdbc.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/testcontainers-jdbc.md) - `@ServiceConnection` quickstart, PostgreSQL, MySQL, etc.
- [references/failsafe-integration-tests.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/failsafe-integration-tests.md) - `*IT` classes silently skipped unless Failsafe is bound; run `./mvnw verify`

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

- [references/instancio.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/instancio.md) - Generate complex test objects (3+ properties)

## Performance & Migration

- [references/context-caching.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/context-caching.md) - Speed up test suites
- [references/sb4-migration.md](https://skilld.dev/api/skills-raw/github/awesome-copilot/spring-boot-testing/references/sb4-migration.md) - Spring Boot 4.0 changes

## 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:**
```java
// 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:

```java
@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>.

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