How can I tell MockRestServiceServer
to expect a specific json
content during a junit @Test
?
I thought I could expect just a json string as follows:
.andExpect(content().json(...))
, but that method does not exist. So how could I rewrite the following?
private MockRestServiceServer mockServer;
private ObjectMapper objectMapper; //jackson
Person p = new Person();
p.setFirstname("first");
p.setLastname("last");
//mocking a RestTemplate webservice response
mockServer.expect(once(), requestTo("/servlet"))
.andExpect(content().json(objectMapper.writeValueAsString(p))) //.json() does not exist! why?
.andRespond(withSuccess(...));
You can use for example:
mockServer.expect(ExpectedCount.once(), requestTo(path))
.andExpect(method(HttpMethod.POST))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].someField").value("some value"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With