Slice test chỉ load một phần ApplicationContext — nhanh hơn @SpringBootTest, isolate layer cần test.
@WebMvcTest — controller layer:
java
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired MockMvc mvc;
@MockBean UserService service;
@Test void getUser() throws Exception {
when(service.find(1L)).thenReturn(new User(1L, "Alice"));
mvc.perform(get("/api/users/1")).andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Alice"));
}
}@DataJpaTest — repository layer:
java
@DataJpaTest
class UserRepositoryTest {
@Autowired UserRepository repo;
@Test void findByEmail() {
repo.save(new User("alice@ex.com"));
assertThat(repo.findByEmail("alice@ex.com")).isPresent();
}
}Slice khác: @JsonTest, @WebFluxTest, @RestClientTest, @DataMongoTest. Chọn: controller → @WebMvcTest; query → @DataJpaTest (+ Testcontainers); full flow → @SpringBootTest.
(Boot 3.4+: @MockBean → @MockitoBean.)