Data REST API Test Code 작성
슬라이스 테스트에서는 작동을 못한다. 이에 따라 통합 테스트로 진행한다.
import org.springframework.test.web.servlet.MockMvc;
@DisplayName("DATA REST API 테스트")
@WebMvcTest
public class DataRestTest {
private final MockMvc mvc;
public DataRestTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[api] 게시글 리스트 조회")
@Test
void givenNothing_whenRequestingArticle_thenReturnsArticlesJsonResponse() throws Exception {
// when & then
mvc.perform(get("/api/articles"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")))
.andDo(print());
}
}
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class DataRestTest {
private final MockMvc mvc;
public DataRestTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[api] 게시글 리스트 조회")
@Test
void givenNothing_whenRequestingArticle_thenReturnsArticlesJsonResponse() throws Exception {
// when & then
mvc.perform(get("/api/articles"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")))
.andDo(print());
// import 안 나올 때 ctrl + space, static 으로 가져올 때는 option + enter
}
@Transactional
을 붙혀 기본적인 방식인 Rollback이 작동하게 만든다.@AutoConfigureMockMvc
@Transactional
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class DataRestTest {
private final MockMvc mvc;
public DataRestTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[api] 게시글 리스트 조회")
@Test
void givenNothing_whenRequestingArticle_thenReturnsArticlesJsonResponse() throws Exception {
// when & then
mvc.perform(get("/api/articles"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.valueOf("application/hal+json")))
.andDo(print());
// import 안 나올 때 ctrl + space, static 으로 가져올 때는 option + enter
}