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.

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

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.