github/awesome-copilot / skills/spring-boot-testing
spring-boot-testing
VerifiedExpert 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.3 KB |
| references/assertj-basics.md | markdown | 4.3 KB |
| references/assertj-collections.md | markdown | 4.9 KB |
| references/context-caching.md | markdown | 2.4 KB |
| references/datajpatest.md | markdown | 5.0 KB |
| references/failsafe-integration-tests.md | markdown | 2.5 KB |
| references/instancio.md | markdown | 4.9 KB |
| references/mockitobean.md | markdown | 4.5 KB |
| references/mockmvc-classic.md | markdown | 5.8 KB |
| references/mockmvc-tester.md | markdown | 7.5 KB |
| references/restclienttest.md | markdown | 4.6 KB |
| references/resttestclient.md | markdown | 4.9 KB |
| references/sb4-migration.md | markdown | 3.3 KB |
| references/test-slices-overview.md | markdown | 5.5 KB |
| references/testcontainers-jdbc.md | markdown | 7.7 KB |
| references/webmvctest.md | markdown | 3.6 KB |
references/webmvctest.md
View on GitHub# @WebMvcTest
Testing Spring MVC controllers with focused slice tests.
## Basic Structure
```java
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired
private MockMvcTester mvc;
@MockitoBean
private OrderService orderService;
@MockitoBean
private UserService userService;
}
```
## What Gets Loaded
- The specified controller(s)
- Spring MVC infrastructure (HandlerMapping, HandlerAdapter)
- Jackson ObjectMapper (for JSON)
- Exception handlers (@ControllerAdvice)
- Spring Security filters (if on classpath)
- Validation (if on classpath)
## Testing GET Endpoints
```java
@Test
void shouldReturnOrder() {
var order = new Order(1L, "PENDING", BigDecimal.valueOf(99.99));
given(orderService.findById(1L)).willReturn(order);
assertThat(mvc.get().uri("/orders/1"))
.hasStatusOk()
.hasContentType(MediaType.APPLICATION_JSON)
.bodyJson()
.extractingPath("$.status")
.isEqualTo("PENDING");
}
```
## Testing POST with Request Body
### Using Text Blocks (Java 25)
```java
@Test
void shouldCreateOrder() {
given(orderService.create(any(OrderRequest.class))).willReturn(1L);
var json = """
{
"product": "Product A",
"quantity": 2
}
""";
assertThat(mvc.post().uri("/orders")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.hasStatus(HttpStatus.CREATED)
.hasHeader("Location", "/orders/1");
}
```
### Using Records
```java
record OrderRequest(String product, int quantity) {}
@Test
void shouldCreateOrderWithRecord() {
var request = new OrderRequest("Product A", 2);
given(orderService.create(any())).willReturn(1L);
assertThat(mvc.post().uri("/orders")
.contentType(MediaType.APPLICATION_JSON)
.content(json.write(request).getJson()))
.hasStatus(HttpStatus.CREATED);
}
```
## Testing Validation Errors
```java
@Test
void shouldRejectInvalidOrder() {
var invalidJson = """
{
"product": "",
"quantity": -1
}
""";
assertThat(mvc.post().uri("/orders")
.contentType(MediaType.APPLICATION_JSON)
.content(invalidJson))
.hasStatus(HttpStatus.BAD_REQUEST)
.bodyJson()
.hasPath("$.errors");
}
```
## Testing Query Parameters
```java
@Test
void shouldFilterOrdersByStatus() {
assertThat(mvc.get().uri("/orders?status=PENDING"))
.hasStatusOk();
verify(orderService).findByStatus(OrderStatus.PENDING);
}
```
## Testing Path Variables
```java
@Test
void shouldCancelOrder() {
assertThat(mvc.put().uri("/orders/123/cancel"))
.hasStatusOk();
verify(orderService).cancel(123L);
}
```
## Testing with Security
```java
@Test
@WithMockUser(roles = "ADMIN")
void adminShouldDeleteOrder() {
assertThat(mvc.delete().uri("/orders/1"))
.hasStatus(HttpStatus.NO_CONTENT);
}
@Test
void anonymousUserShouldBeForbidden() {
assertThat(mvc.delete().uri("/orders/1"))
.hasStatus(HttpStatus.UNAUTHORIZED);
}
```
## Multiple Controllers
```java
@WebMvcTest({OrderController.class, ProductController.class})
class WebLayerTest {
// Tests multiple controllers in one slice
}
```
## Excluding Auto-Configuration
```java
@WebMvcTest(OrderController.class)
@AutoConfigureMockMvc(addFilters = false) // Skip security filters
class OrderControllerWithoutSecurityTest {
// Tests without security filters
}
```
## Key Points
1. Always mock services with @MockitoBean
2. Use MockMvcTester for AssertJ-style assertions
3. Test HTTP semantics (status, headers, content-type)
4. Verify service method calls when side effects matter
5. Don't test business logic here - that's for unit tests
6. Leverage Java 25 text blocks for JSON payloads
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |