-1
MvcResult mvcResult = mockMvc.perform(post("/user/addItemIntoCart").andExpect(status().isOk()).andReturn();
@PostMapping(value = "/addItemIntoCart",consumes = {"application/json"})
public ResponseEntity<String> addItemToCart(@RequestBody CartDto cartDto) {
   return cartService.addItemIntoCart(cartDto);
}

how can I pass object of CartDto to the mockmvc

I am trying to test addItemIntoCart method, but not sure how to pass object to the mockmvc

1 Answers1

0

Here you can use the answer that contains what you want: https://stackoverflow.com/a/35402975/11988137


In a nutshell, you need to create the object and using objectMapper, convert it to a a json string. Then you need to use contentType() and content() method in order to send the json string you've created to the api.

patrinox
  • 76
  • 7