JuyeoungJun / cron-monitoring

for cron-monitoring
0 stars 0 forks source link

MockMVC JSONBody 전달 방식 #50

Closed JuyeoungJun closed 3 years ago

JuyeoungJun commented 3 years ago

In GitLab by @gm2202981 on Jun 18, 2021, 16:58

잭슨 매퍼 방식

ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); ObjectWriter ow = mapper.writer(); String dto = ow.writeValueAsString(servers);

mockMvc.perform(get("/cron-servers")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string(dto));


# JsonPath 방식
- 참고링크: https://github.com/json-path/JsonPath, https://ykh6242.tistory.com/100
```java
List<CronServerDTO> servers = new ArrayList<>();
servers.add(new CronServerDTO("1"));
servers.add(new CronServerDTO("2"));
given(cronServerService.getCronServers()).willReturn(servers);

String expectAllServerIp = "$.[*]";
mockMvc.perform(get("/cron-servers"))
    .andDo(print())
    .andExpect(status().isOk())
    .andExpect(jsonPath(expectAllServerIp, servers).exists());