태그:

카테고리:

업데이트:


Spring Web MVC Test하기

@SpringBootTest
@AutoConfigureMockMvc
class MyControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void myText() throws Exception {
        MyDTO sample = MyDTO.sample();
        String contents = objectMapper.writeValueAsString(sample);

        mockMvc.perform(post("/users") // post 요청
            .content(contents) // parameter
            .contentType(MediaType.APPLICATION_JSON) // contents type
            .accept(MediaType.APPLICATION_JSON)) // accept type
            .andExpect(status().isOk()) // status 검사
            .andExpect(content().string(sample.getId())) // 반환 결과가 sample의 id인지 검사
            .andDo(print()); // console에 출력
            
    }

}

댓글남기기