I'm using Mock-Server (mock-server.com) in version 5.10.0 for mocking a REST-API in a JUnit 5 test. I'm developing a spring-boot application with Java 8. My payload format is "protobuf" and so my REST interface is returning a ResponseEntity<MyProtoObject>
. Unfortunately I can't find a built-in solution in Mock-Server that custom objects are returned by the Mock-Server as body. I only found methods like withBody(String)
or withBody(byte[])
. In addition to that methods I found a method withBody(BodyWithContentType)
. I tried to create the following implementation/usage of the abstract class BodyWithContent<T>
in my unit testclass:
private final class MyProtoObjectPayloadBody extends BodyWithContentType<MyProtoObject> {
public MyProtoObjectPayloadBody () {
super(null, null);
}
@Override
public MyProtoObject getValue() {
// return logic of MyProtoObject
}
}
// Called in @BeforeEach
private void prepareMock() {
this.mockServer.when(HttpRequest.request().withMethod("GET").withPath("/my-path"))
.respond(HttpResponse.response().withStatusCode(200).withBody(new MyProtoObjectPayloadBody ()).withDelay(TimeUnit.SECONDS, 5));
}
When I debug my code where the ResponseEntity occurs I get the response from the mock server but the "body" of the response is always empty. My custom getValue()
is never called, too. I ensured that the REST call result in my application logic comes from the mock server using different return codes.
Can someone help me with this problem?
Unfortunately I got the reply from the mock-server developer via Slack that it's not possible to return custom objects. It's unclear to the mock-server how they should be (de)serialized. So I'll try to use a workaround via byte[]