How to check JSON in response body with mockMvc

前端 未结 3 772
野趣味
野趣味 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:03

    You can try the below for get and post methods

    @Autowired
    private MuffinRepository muffinRepository;
    
    @Test
    public void testgetMethod throws Exception(){
        Muffin muffin = new Muffin("Butterscotch");
        muffin.setId(1L);
    
        BddMockito.given(muffinRepository.findOne(1L)).
            willReturn(muffin);
    
        mockMvc.perform(MockMvcRequestBuilders.
            get("/muffins/1")).
            andExpect(MockMvcResutMatchers.status().isOk()).
            andExpect(MockMvcResutMatchers.content().string("{\"id\":1, "flavor":"Butterscotch"}"));    
    }
    
    //Test to do post operation
    @Test
    public void testgetMethod throws Exception(){
        Muffin muffin = new Muffin("Butterscotch");
        muffin.setId(1L);
    
        BddMockito.given(muffinRepository.findOne(1L)).
            willReturn(muffin);
    
        mockMvc.perform(MockMvcRequestBuilders.
            post("/muffins")
            .content(convertObjectToJsonString(muffin))
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResutMatchers.status().isCreated())
            .andExpect(MockMvcResutMatchers.content().json(convertObjectToJsonString(muffin))); 
    }
    

    If the response is empty then make sure to override equals() and hashCode() methods on the Entity your repository is working with:

    //Converts Object to Json String
    private String convertObjectToJsonString(Muffin muffin) throws JsonProcessingException{
        ObjectWriter writer = new ObjectWriter().writer().withDefaultPrettyPrinter();
        return writer.writeValueAsString(muffin);
    }
    
    0 讨论(0)
  • 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.*;

    0 讨论(0)
  • 2020-12-30 19:15

    The 406 Not Acceptable status code means that Spring couldn't convert the object to json. You can either make your controller method return a String and do return json.toString(); or configure your own HandlerMethodReturnValueHandler. Check this similar question Returning JsonObject using @ResponseBody in SpringMVC

    0 讨论(0)
提交回复
热议问题