Bali
Bali

Reputation: 825

How to retrieve String from jsonPath() in MockMvc

I would like to compare if two jsonPath value are equal like this:

this.mockMvc.perform(get(requestURL)).andExpect(jsonPath("$.prop1", equalTo(jsonPath("$.prop2"))));

but then my test was failed. The jsonPath("$.prop1") returned the correct value I want, but jsonPath("$.prop2") returned not the value of this property, instead the classname like:

org.springframework.test.web.servlet.result.JsonPathResultMatchers@7b7aaaf6

Can anyone give me idea how can I perform the toString() method for the jsonPath()? I did try also jsonPath("$.prop2").toString() but also received the classname.

Thank you in advanced!

Upvotes: 5

Views: 3925

Answers (1)

Aleksander Burzec
Aleksander Burzec

Reputation: 370

MvcResult result = this.mockMvc.perform(get(requestURL)).andReturn();
String response = result.getResponse().getContentAsString();

assertEquals(JsonPath.parse(response).read("$.prop1").toString(),JsonPath.parse(response).read("$.prop2").toString());

See github readme for more details.

Upvotes: 7

Related Questions