0

我正在尝试为使用 Jersey 客户端发出请求的 API 客户端编写测试。

我想伪造某个服务器响应以返回预先捕获的 json 字符串。

例如。

client().resource("/recommendations").queryParam("username", karan").get(Recommendation.class)

应该根据我存储在文件中的 json 字符串返回适当的类。

我怎么能假装呢?或者我是否必须实例化一个假服务器来返回实际的 json,并让球衣客户端来完成它的工作?

谢谢

4

1 回答 1

1

One popular solution is to use a testing framework like EasyMock or Mockito to create a mock Jersey client which expects specific method calls and returns predefined data (e.g. json). The mock is then injected into the API client in place of the real Jersey client.

In general, you can also avoid the frameworks by creating the mock yourself, i.e. subclassing the client and overriding methods you expect to call, to return predefined data. Then pass your mock into the API client as a constructor argument. Whether or not you justify a framework depends on how much mocking you expect to need, which is determined in part by how many external dependencies you have.

于 2013-11-09T17:13:13.160 回答