1

Is it possible to use MockMCV to compare the actual ResponseEntity and the one returned from the controller?

@Test
public void testStatusGetAllCars() throws Exception {
    ResponseEntity<?> expectedResponse = carController.getAllCars();

    mockMVC.perform(MockMvcRequestBuilders.get("/api/cars"))
    .andDo(MockMvcResultHandlers.print())
    .andExpect(MockMvcResultMatchers.status().isOk())
    .andExpect(MockMvcResultMatchers.content().contentType("application/hal+json;charset=UTF-8"))
    .andExpect( /// compare goes here // );

}
Eray Tuncer
  • 707
  • 2
  • 11
  • 31
  • Have you tried something like this? https://stackoverflow.com/questions/18336277/how-to-check-string-in-response-body-with-mockmvc You can convert your expectedResponse to String and compare those 2 objects like Strings. – Spasoje Petronijević Aug 13 '19 at 12:44

1 Answers1

0

controller returns json as ResponseEntity's body. you can use jackson and create your expected json Object using NodeObject and Objectmapper

see https://attacomsian.com/blog/jackson-create-json-object

ObjectMapper mapper = new ObjectMapper();

NodeObject expected = mapper.createObjectNode();
//then put your fields

now you can use

.andExpect(MockMvcResultMatchers.content().json(expected.toString());

it worked for me

rezaMK
  • 1
  • 1