Back to Skills

spring-boot-testing

verified

Spring Boot 4 testing strategies and patterns. Use when writing unit tests, slice tests (@WebMvcTest, @DataJpaTest), integration tests, Testcontainers with @ServiceConnection, security testing (@WithMockUser, JWT), or Modulith event testing with Scenario API. Covers the critical @MockitoBean migration from @MockBean.

View on GitHub

Marketplace

arkhe-claude-plugins

joaquimscosta/arkhe-claude-plugins

Plugin

spring-boot

Repository

joaquimscosta/arkhe-claude-plugins
3stars

plugins/spring-boot/skills/spring-boot-testing/SKILL.md

Last Verified

January 23, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/joaquimscosta/arkhe-claude-plugins/blob/main/plugins/spring-boot/skills/spring-boot-testing/SKILL.md -a claude-code --skill spring-boot-testing

Installation paths:

Claude
.claude/skills/spring-boot-testing/
Powered by add-skill CLI

Instructions

# Spring Boot 4 Testing

Comprehensive testing patterns including slice tests, Testcontainers, security testing, and Modulith Scenario API.

## Critical Breaking Change

| Old (Boot 3.x) | New (Boot 4.x) | Notes |
|----------------|----------------|-------|
| `@MockBean` | `@MockitoBean` | **Required migration** |
| `@SpyBean` | `@MockitoSpyBean` | **Required migration** |
| `MockMvc` (procedural) | `MockMvcTester` (fluent) | **New AssertJ-style API** |
| Implicit `@AutoConfigureMockMvc` | Explicit annotation required | Add to `@SpringBootTest` |

## MockMvcTester (Spring Boot 4)

New fluent, AssertJ-style API for controller testing:

```java
@WebMvcTest(UserController.class)
class UserControllerTest {

    @Autowired
    private MockMvcTester mvc;  // NEW: Fluent API

    @Test
    void getUser_returnsUser() {
        mvc.get().uri("/users/{id}", 1)
            .exchange()
            .assertThat()
            .hasStatusOk()
            .bodyJson()
            .extractingPath("$.name")
            .isEqualTo("John");
    }
}
```

**Key Benefits**: Fluent assertions, better error messages, AssertJ integration.

## Test Annotation Selection

| Test Type | Annotation | Use When |
|-----------|------------|----------|
| Controller | `@WebMvcTest` | Testing request/response, validation |
| Repository | `@DataJpaTest` | Testing queries, entity mapping |
| JSON | `@JsonTest` | Testing serialization/deserialization |
| REST Client | `@RestClientTest` | Testing external API clients |
| Full Integration | `@SpringBootTest` | End-to-end, with real dependencies |
| Module | `@ApplicationModuleTest` | Testing bounded context in isolation |

## Core Workflow

1. **Choose test slice** → Minimal context for fast tests
2. **Mock dependencies** → `@MockitoBean` for external services
3. **Use Testcontainers** → `@ServiceConnection` for databases
4. **Assert thoroughly** → Use AssertJ, `MockMvcTester` (new), `RestTestClient` (new), WebTestClient
5. **Test security** → `@WithMockUser`,

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
3879 chars