Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 291
Sasha Shpota Avatar asked Aug 02 '19 14:08

Sasha Shpota


1 Answers

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"))

like image 181
user7294900 Avatar answered Sep 19 '22 04:09

user7294900