Mock-Server – 自定义对象作为 REST 响应

huangapple go评论62阅读模式
英文:

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));
}

当我调试代码以查看ResponseEntity发生的地方时,我可以从模拟服务器获得响应,但响应的“body”始终为空。我的自定义getValue()也从未被调用过。我确保应用逻辑中的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&lt;MyProtoObject&gt;. 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&lt;T&gt; in my unit testclass:

private final class MyProtoObjectPayloadBody extends BodyWithContentType&lt;MyProtoObject&gt; {

    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(&quot;GET&quot;).withPath(&quot;/my-path&quot;))
                .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(&lt;URL&gt;))
            .andRespond(MockRestResponseCreators.withSuccess(
                &lt;json-response&gt;, MediaType.APPLICATION_JSON))

huangapple
  • 本文由 发表于 2020年4月9日 22:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61123230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定