0

I'm writing mockMvc tests for my controller, and need to validate jsonPath return value.

Have tried differently with .is() and .value(), mapping, in any way i can imagine with no success

Loan loan = new Loan(
                "0000-0000",
                "OPEN",
                LocalDate.now(),
                LocalDate.now().plusDays(30),
                new BigDecimal("500.0"),
                new BigDecimal("50.0"),
                new BigDecimal("550.0"),
                new ArrayList<>()
        );

    Mockito.lenient()
            .when(loanService.loans())
            .thenReturn(Collections.singletonList(loan));

    String json = MAPPER.writeValueAsString(loan);

    mockMvc.perform(get("/api/loans"))
            .andExpect(jsonPath("$.*").value(json));



Expected :{"id":"0000-0000","status":"OPEN","created":"2019-05-09","dueDate":"2019-06-08","principal":500.0,"interest":50.0,"total":550.0,"extensions":[]}
Actual   :{id=0000-0000, status=OPEN, created=2019-05-09, dueDate=2019-06-08, principal=500.0, interest=50.0, total=550.0, extensions=[]}

So this is the closes i got, just dont get the types here.

nilino
  • 1
  • How does the response look when you hit the endpoint on postman? – Madhu Bhat May 09 '19 at 09:16
  • postman response [ { "id": "8706-2150", "status": "OPEN", "created": "2019-05-09", "dueDate": "2019-06-08", "principal": 400, "interest": 40, "total": 440, "extensions": [] } ] – nilino May 09 '19 at 09:22

1 Answers1

1

In case you want to assert the complete responseBody as a json, you may use MockMvcResultMatchers's content method.

Just replace jsonPath("$.*").value(json) with content().json(json) as below

mockMvc.perform(get("/api/loans"))
            .andExpect(content().json(json));
Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54