๊ฐ์์์ api controller test ์ mockMvc๋ฅผ ํ์ฉํด์ ๊ฒฐ๊ณผ์ ๋ํ ๊ฒ์ฆ์ status ์ ๋๋ก๋ง ํ๊ณ ์๋๋ฐ
jsonPath
๋ฅผ ํ์ฉํด์ ์๋ต๋ฐ์ json์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆํ ์ ์์ต๋๋ค.$๋ก ์์ํ๋ json path ํํ์์ผ๋ก ๊ฒ์ฆํ๊ณ ์ ํ๋ value๊ฐ์ ์ ํํ์ฌ ๊ฒ์ฆํ ์ ์์ต๋๋ค.
ํ์ฉ์์
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @WebMvcTest class VoucherRestControllerTest { @Autowired private MockMvc mockMvc; @MockBean private VoucherService voucherService; @Test @DisplayName("[API][GET] ๋ฐ์ฐ์ฒ ๋ฆฌ์คํธ ์กฐํ") void getAllVouchers() throws Exception { given(voucherService.getVouchers()).willReturn(List.of(fixedVoucher, percentVoucher, fixedVoucher2)); mockMvc.perform(get("/api/v1/voucher").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].voucherId").value(fixedVoucher.getVoucherId().toString())) .andExpect(jsonPath("$[1].voucherId").value(percentVoucher.getVoucherId().toString())) .andExpect(jsonPath("$[2].voucherId").value(fixedVoucher2.getVoucherId().toString())) .andDo(print()); } }
- jsonPath์์ ํจํด ๋งค์นญ ์ฌ์ฉ๋ฐฉ๋ฒ ์ฐธ๊ณ :