0

Here is my endpoint :

 @PostMapping("/email/registration")
  public Data registration(@RequestBody Data data, HttpServletRequest 
  httpServletRequest) throws Exception {
  emailSenderService.pushEmail(httpServletRequest.getRequestURI(),          
  emailSenderService.sendConfirmationEmail(httpServletRequest.getRequestURI(), data));
  emailSenderService.consumeEmail(httpServletRequest.getRequestURI());
  return data;
  }

And here is my test(of course it's not working) :

  @Test
  public void testRegistrationConfirmation() throws Exception {
      Data testData = new Data();
      when(testData).thenReturn(testData);
      mockMvc.perform(
              post("/email/registration")
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(asJsonString(testData)))
              .andExpect(status().isOk())
              .andExpect(testData);
  }

I would like to see if the give object is the same as the return object, preferably without filling all the fields with values.

Thank you for the answers!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vitya
  • 1
  • Possible duplicate of [How to check JSON in response body with mockMvc](https://stackoverflow.com/questions/30482934/how-to-check-json-in-response-body-with-mockmvc) – QBrute Jul 20 '17 at 09:19

1 Answers1

0

It could be more elegant but you can use Jackson to make the json for you:

@Test
  public void testRegistrationConfirmation() throws Exception {
      Data testData = new Data();
      when(testData).thenReturn(testData);
      mockMvc.perform(
              post("/email/registration")
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(asJsonString(testData)))
              .andExpect(status().isOk())
              .andExpect(content().string(equalTo(new ObjectMapper().writeValueAsString(testData))));
  }
Essex Boy
  • 7,565
  • 2
  • 21
  • 24