【问题标题】:Mock Rest Server Response for Unit Testing用于单元测试的模拟 Rest 服务器响应
【发布时间】:2017-04-12 00:49:21
【问题描述】:

在集成测试中,我使用真正的远程服务器来使用 REST API。 在不依赖外部实体的单元测试中提供这些响应的最简单方法是什么。

一种可能的方法是构建

public class TestHttpResponse implements   org.apache.http.client.methods.CloseableHttpResponse

override

 @Override
    public StatusLine getStatusLine() {
      return new StatusLine() {
        @Override
        public ProtocolVersion getProtocolVersion() {
          return null;
        }

        @Override
        public int getStatusCode() {
          return statusCode;
        }

        @Override
        public String getReasonPhrase() {
          return reasonPhrase;
        }
      };
    }
   ...

有没有更简单更好的方法来模拟 REST API 响应负载?

【问题讨论】:

  • 你试过使用养蜂场吗? apiary.io 它有模拟选项
  • 看起来像商业广告。我想要连接在 UT 中的简单代码。你有在你的 UT 中使用它的例子吗?

标签: rest unit-testing mockrestserviceserver


【解决方案1】:

为此,我是 Mockito 的粉丝:

http://www.vogella.com/tutorials/Mockito/article.html#testing-with-mock-objects

Mockito 的优点在于您可以动态控制它的行为。

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 2021-05-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2016-04-27
    相关资源
    最近更新 更多