MockRestServiceServer: how to mock a POST call with a body?

Sasha Shpota picture Sasha Shpota · Aug 2, 2019 · Viewed 9k times · Source

I am trying to mock a POST method with MockRestServiceServer in the following way:

MockRestServiceServer server = bindTo(restTemplate).build();
server.expect(requestTo("/my-api"))
        .andExpect(method(POST))
        .andRespond(withSuccess(expectedResponce, APPLICATION_JSON));

Problem: How do I verify a request body in this setup?

I browsed through the documentation and some examples and still can't figure out how it can be done.

Answer

user7294900 picture user7294900 · Aug 5, 2019

You can use content().string to verify body:

.andExpect(content().string(expectedContent))

Or content().bytes:

this.mockServer.expect(content().bytes("foo".getBytes()))

this.mockServer.expect(content().string("foo"))