ワンクリックで
spring-testing
Spring Boot testing with @SpringBootTest, MockMvc, and Testcontainers
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Spring Boot testing with @SpringBootTest, MockMvc, and Testcontainers
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | spring-testing |
| description | Spring Boot testing with @SpringBootTest, MockMvc, and Testcontainers |
Reliable tests for Spring Boot services and APIs. Prefer slice tests for speed, full context when needed.
Read only files relevant to the request. Use the content map to focus.
| File | Description | When to Read |
|---|---|---|
slice-tests.md | @WebMvcTest, @DataJpaTest | Fast feedback |
integration.md | @SpringBootTest, Testcontainers | Real dependencies |
mockmvc.md | MockMvc usage | Controller tests |
test-data.md | Test fixtures, builders | Clean data setup |
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Test
void getUser_returnsUser() throws Exception {
when(userService.findById("u1"))
.thenReturn(Optional.of(new User("u1", "dev@example.com")));
mockMvc.perform(get("/api/v1/users/u1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email").value("dev@example.com"));
}
}
@DataJpaTest
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
void findByEmail_returnsUser() {
var user = userRepository.save(new User("u1", "dev@example.com"));
assertThat(userRepository.findByEmail("dev@example.com")).isPresent();
}
}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
class UserControllerIT {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15");
@Autowired
private TestRestTemplate restTemplate;
@Test
void createUser_returnsCreated() {
var request = new CreateUserRequest("dev@example.com", "Dev");
var response = restTemplate.postForEntity("/api/v1/users", request, UserResponse.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
}
}
| Anti-Pattern | Why Bad | Better Approach |
|---|---|---|
| @SpringBootTest for all tests | Slow | Slice tests |
| Real DB without isolation | Flaky | Testcontainers |
| Static mutable fixtures | Hidden coupling | Builders/per-test setup |
| Need | Skill |
|---|---|
| Core Spring patterns | @[skills/spring-boot-patterns] |
| Data access | @[skills/spring-data-jpa] |
Core Java patterns, modern Java features (JDK 17+), best practices
Maven and Gradle build tools, dependency management, multi-module projects
JUnit 5 testing patterns with Mockito and AssertJ
Spring Boot 3 patterns for controllers, services, configuration, and validation
Spring Data JPA and Hibernate patterns for entities, repositories, and query performance
Spring Security 6 patterns for authentication, authorization, and OAuth2