Test Spring Boot applications - MockMvc, TestContainers, test slices, integration testing
Test Spring Boot applications using MockMvc, TestContainers, and test slices. Triggers when writing or debugging Spring tests for controllers, repositories, or integration scenarios.
/plugin marketplace add pluginagentmarketplace/custom-plugin-spring-boot/plugin install spring-boot-assistant@pluginagentmarketplace-spring-bootThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlassets/schema.jsonreferences/GUIDE.mdreferences/PATTERNS.mdscripts/validate.pyMaster testing Spring Boot applications with MockMvc, TestContainers, test slices, and comprehensive testing strategies.
This skill covers the complete testing pyramid for Spring Boot applications from unit tests to integration tests.
| Name | Type | Required | Default | Validation |
|---|---|---|---|---|
test_type | enum | ✗ | unit | unit | integration | e2e |
slice | enum | ✗ | - | web | data | json |
mock_strategy | enum | ✗ | mockito | mockito | mockbean | wiremock |
@WebMvcTest, @DataJpaTest, @JsonTest@WithMockUser@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Test
void shouldReturnUser() throws Exception {
when(userService.findById(1L)).thenReturn(new UserResponse(1L, "John"));
mockMvc.perform(get("/api/users/{id}", 1L))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("John"));
}
@Test
void shouldReturn400WhenInvalid() throws Exception {
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"\"}"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.errors.name").exists());
}
@Test
@WithMockUser(roles = "ADMIN")
void shouldAllowAdminAccess() throws Exception {
mockMvc.perform(get("/api/admin/users"))
.andExpect(status().isOk());
}
}
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Testcontainers
class UserRepositoryTest {
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15");
@Autowired
private UserRepository userRepository;
@Autowired
private TestEntityManager entityManager;
@Test
void shouldFindByEmail() {
User user = new User("john@test.com", "John");
entityManager.persistAndFlush(user);
Optional<User> found = userRepository.findByEmail("john@test.com");
assertThat(found).isPresent()
.hasValueSatisfying(u -> assertThat(u.getName()).isEqualTo("John"));
}
}
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Testcontainers
class OrderIntegrationTest {
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15");
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private OrderRepository orderRepository;
@BeforeEach
void setUp() {
orderRepository.deleteAll();
}
@Test
void shouldCreateOrder() {
CreateOrderRequest request = new CreateOrderRequest("item1", 2);
ResponseEntity<OrderResponse> response = restTemplate
.postForEntity("/api/orders", request, OrderResponse.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody().id()).isNotNull();
assertThat(orderRepository.findAll()).hasSize(1);
}
}
@SpringBootTest
@WireMockTest(httpPort = 8089)
class PaymentServiceTest {
@Autowired
private PaymentService paymentService;
@Test
void shouldProcessPayment() {
stubFor(post("/payments")
.willReturn(okJson("{\"transactionId\":\"TXN-123\",\"status\":\"SUCCESS\"}")));
PaymentResult result = paymentService.charge(new BigDecimal("99.99"));
assertThat(result.transactionId()).isEqualTo("TXN-123");
assertThat(result.status()).isEqualTo("SUCCESS");
}
}
| Issue | Diagnosis | Fix |
|---|---|---|
| Context not loading | Missing config | Add @TestConfiguration |
| MockBean not working | Wrong package | Check component scan |
| TestContainers timeout | Docker not running | Start Docker daemon |
□ Check test annotations (@SpringBootTest, @WebMvcTest)
□ Verify MockBean setup
□ Confirm TestContainers are starting
□ Check application-test.yml is loaded
□ Enable debug logging for tests
# application-test.yml
spring:
jpa:
show-sql: true
logging:
level:
org.springframework.test: DEBUG
org.testcontainers: DEBUG
Skill("spring-testing")
| Version | Date | Changes |
|---|---|---|
| 2.0.0 | 2024-12-30 | TestContainers 1.19+, WireMock 3.x patterns |
| 1.0.0 | 2024-01-01 | Initial release |
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.