Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert json content in MockRestServiceServer?

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(...));
like image 934
membersound Avatar asked Mar 14 '18 16:03

membersound


1 Answers

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"))
like image 109
Omri Aviv Avatar answered Oct 20 '22 00:10

Omri Aviv