英文:
Mock-Server - Custom object as REST response
问题
我正在使用版本为5.10.0的Mock-Server(mock-server.com)来模拟JUnit 5测试中的REST API。我正在使用Java 8开发spring-boot应用程序。我的有效负载格式是“protobuf”,因此我的REST接口返回ResponseEntity<MyProtoObject>
。不幸的是,在Mock-Server中我找不到内置的解决方案,以便由Mock-Server返回自定义对象作为响应体。我只找到了诸如withBody(String)
或withBody(byte[])
之类的方法。除了这些方法之外,我还找到了一个名为withBody(BodyWithContentType)
的方法。我尝试在我的单元测试类中创建以下对抽象类BodyWithContent<T>
的实现/用法:
private final class MyProtoObjectPayloadBody extends BodyWithContentType<MyProtoObject> {
public MyProtoObjectPayloadBody () {
super(null, null);
}
@Override
public MyProtoObject getValue() {
// 返回MyProtoObject的逻辑
}
}
// 在@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));
}
当我调试代码以查看ResponseEntitygetValue()
也从未被调用过。我确保应用逻辑中的REST调用结果来自使用不同返回代码的模拟服务器。
有人可以帮助我解决这个问题吗?
英文:
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<MyProtoObject> 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?
答案1
得分: 0
很不幸,我通过Slack从模拟服务器开发者那里得到的回复是,无法返回自定义对象。模拟服务器不清楚如何对这些对象进行序列化和反序列化。所以我将尝试通过byte[]来使用一个解决方法。
英文:
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[]
答案2
得分: 0
你可以将你的对象的 JSON 响应返回,如下所示:
mockServer.expect(ExpectedCount.once(),
MockRestRequestMatchers.requestTo(<URL>))
.andRespond(MockRestResponseCreators.withSuccess(
<json-response>, MediaType.APPLICATION_JSON))
英文:
You can return json response of your object as
mockServer.expect(ExpectedCount.once(),
MockRestRequestMatchers.requestTo(<URL>))
.andRespond(MockRestResponseCreators.withSuccess(
<json-response>, MediaType.APPLICATION_JSON))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论