mercredi 5 août 2020

How to test Json correctly using mockmvc (Java)? What am I doing wrong?

I'm just starting to learn Spring The controller accepts and returns Json. I can't test it.

Controller

@RequestMapping(value = "",
            method = RequestMethod.GET,
            headers = "Accept=application/json")
    @ResponseBody
    public String getObject(@RequestBody String json) throws JsonProcessingException {
        Order order = convertService.JsonToOrder(json);
        return orderService.findOrderListByCustomer(order);
    }

ControllerTest

 @Test
    void getJsonTest() throws Exception {

        mvc.perform(MockMvcRequestBuilders
                .get("/sb/service")
                .content("{\"customer\":\"user1\",\"cost\":23}")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }

When testing, I always get 404. But if I send a request from Postman, the response is 200. I studied this answer, but it didn't help How to check JSON response in Spring MVC test

Aucun commentaire:

Enregistrer un commentaire