Let's see a test, which is using MockServer (org.mock-server:mockserver-netty:5.10.0
) for mocking responses.
It is expected that the response body will be equal to string "something"
.
Nevertheless, this test fails, because the response body is an empty string.
@Test
void test1() throws Exception {
var server = ClientAndServer.startClientAndServer(9001);
server
.when(
request().withMethod("POST").withPath("/checks/"),
exactly(1)
)
.respond(
response()
.withBody("\"something\"")
.withStatusCode(205)
.withHeader("Content-Type", "application/json")
);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:9001/checks/"))
.POST(BodyPublishers.noBody())
.build();
HttpResponse<String> response =
HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
assertEquals(205, response.statusCode());
assertEquals("something", response.body()); // fails
}
How to make the response body be equal to the string provided in response().withBody(...)
?