How to check JSON in response body with mockMvc

前端 未结 3 771
野趣味
野趣味 2020-12-30 18:53

This is my method inside my controller which is annotated by @Controller

@RequestMapping(value = \"/getServerAlertFilters/{serverName}/\", produ         


        
3条回答
  •  有刺的猬
    2020-12-30 19:15

    I use TestNG for my unit testing. But in Spring Test Framework they both looks similar. So I believe your test be like below

    @Test
    public void testAlertFilterView() throws Exception {
        this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").
                .andExpect(status().isOk())
                .andExpect(content().json("{'data':[{'useRegEx':'false','hosts':'v2v2v2'}]}"));
        }
    

    If you want check check json Key and value you can use jsonpath .andExpect(jsonPath("$.yourKeyValue", is("WhatYouExpect")));

    You might find thatcontent().json() are not solveble please add

    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

提交回复
热议问题