吉姆和我在上周一直在做一些工作,其中涉及调用neo4j的HA状态URI来检查实例是否是主/从属,并且我们一直在使用jersey-client 。
该代码大致如下所示:

class Neo4jInstance {private Client httpClient;private URI hostname;public Neo4jInstance(Client httpClient, URI hostname) {this.httpClient = httpClient;this.hostname = hostname;}public Boolean isSlave() {String slaveURI = hostname.toString() + ":7474/db/manage/server/ha/slave";ClientResponse response = httpClient.resource(slaveURI).accept(TEXT_PLAIN).get(ClientResponse.class);return Boolean.parseBoolean(response.getEntity(String.class));}
}

在针对该代码编写一些测试时,我们希望保留对HA从URI的实际调用,以便我们可以模拟这两种情况,并且进行简短搜索表明,使用Mockito是可行的方法 。

我们最终进行了如下所示的测试:

@Test
public void shouldIndicateInstanceIsSlave() {Client client = mock( Client.class );WebResource webResource = mock( WebResource.class );WebResource.Builder builder = mock( WebResource.Builder.class );ClientResponse clientResponse = mock( ClientResponse.class );when( builder.get( ClientResponse.class ) ).thenReturn( clientResponse );when( clientResponse.getEntity( String.class ) ).thenReturn( "true" );when( webResource.accept( anyString() ) ).thenReturn( builder );when( client.resource( anyString() ) ).thenReturn( webResource );Boolean isSlave = new Neo4jInstance(client, URI.create("http://localhost")).isSlave();assertTrue(isSlave);
}

这很粗糙,但是可以完成工作。

我认为应该有更好的方法,所以我继续搜索,最终在邮件列表中发现了此帖子,该帖子建议创建一个自定义ClientHandler并在其中存储请求/响应。

我可以这样做,并用一个仅覆盖我们非常具体的用例的小DSL进行包装:

private static ClientBuilder client() {return new ClientBuilder();
}static class ClientBuilder {private String uri;private int statusCode;private String content;public ClientBuilder requestFor(String uri) {this.uri = uri;return this;}public ClientBuilder returns(int statusCode) {this.statusCode = statusCode;return this;}public Client create() {return new Client() {public ClientResponse handle(ClientRequest request) throws ClientHandlerException {if (request.getURI().toString().equals(uri)) {InBoundHeaders headers = new InBoundHeaders();headers.put("Content-Type", asList("text/plain"));return createDummyResponse(headers);}throw new RuntimeException("No stub defined for " + request.getURI());}};}private ClientResponse createDummyResponse(InBoundHeaders headers) {return new ClientResponse(statusCode, headers, new ByteArrayInputStream(content.getBytes()), messageBodyWorkers());}private MessageBodyWorkers messageBodyWorkers() {return new MessageBodyWorkers() {public Map<MediaType, List<MessageBodyReader>> getReaders(MediaType mediaType) {return null;}public Map<MediaType, List<MessageBodyWriter>> getWriters(MediaType mediaType) {return null;}public String readersToString(Map<MediaType, List<MessageBodyReader>> mediaTypeListMap) {return null;}public String writersToString(Map<MediaType, List<MessageBodyWriter>> mediaTypeListMap) {return null;}public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> tClass, Type type, Annotation[] annotations, MediaType mediaType) {return (MessageBodyReader<T>) new StringProvider();}public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> tClass, Type type, Annotation[] annotations, MediaType mediaType) {return null;}public <T> List<MediaType> getMessageBodyWriterMediaTypes(Class<T> tClass, Type type, Annotation[] annotations) {return null;}public <T> MediaType getMessageBodyWriterMediaType(Class<T> tClass, Type type, Annotation[] annotations, List<MediaType> mediaTypes) {return null;}};}public ClientBuilder content(String content) {this.content = content;return this;}
}

如果我们更改测试以使用此代码,则现在看起来像这样:

@Test
public void shouldIndicateInstanceIsSlave() {Client client = client().requestFor("http://localhost:7474/db/manage/server/ha/slave").returns(200).content("true").create();Boolean isSlave = new Neo4jInstance(client, URI.create("http://localhost")).isSlave();assertTrue(isSlave);
}

有没有更好的办法?

在Ruby中,我使用WebMock来实现这一目标,而Ashok 则将我指向WebStub ,它看起来不错,除了我需要传递主机名+端口而不是在代码中构造它。

参考: 泽西岛客户:在Mark Needham博客博客中测试 JCG合作伙伴 Mark Needham的外部呼叫 。

翻译自: https://www.javacodegeeks.com/2013/08/jersey-client-testing-external-calls.html

泽西岛客户:测试外部呼叫相关推荐

  1. restful web_泽西岛的RESTful Web服务

    restful web 我已经讨论了有关体系结构考虑事项<< link >>的早期文章,以成为可在我的系统/机器上使用的分布式环境上的RESTful系统. 本文我们将讨论如何基 ...

  2. 泽西岛的RESTful Web服务

    我已经讨论了有关体系结构注意事项<< link >>的早期文章,以成为可在我的系统/机器上使用的分布式环境上的RESTful系统. 本文我们将讨论如何基于REST体系结构考虑来 ...

  3. php客户投诉系统,呼叫中心在线客服系统这样处理客户投诉,满意度提升200%!...

    呼叫中心在线客服系统这样处理客户投诉,满意度提升200%! 很多在线客服害怕客户过来投诉,因为一旦处理不好,小到影响自己的业绩,大到为企业带来负面!所以处理客户的投诉不是那么容易,需要丰富的在线客服经 ...

  4. 泽西岛的JSON模式生成

    因此,在上一篇文章中,我讨论了一个允许在WADL中使用JSON-Schema的建议,这篇文章探讨了如何使它与最近构建的Jersey一起使用. 在1.16发布之前,您将必须下载/参考1.16SNAPSH ...

  5. stm32l4 外部中断按键会卡死_STM32学习之路-按键中断测试(外部中断)

    void RCC_Config(void) { SystemInit(); //系统时钟初始化 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); ...

  6. wi-fi_Google语音正在测试Wi-Fi呼叫,无需呼叫转移

    wi-fi Google Voice is adding a VoIP option on Android and the web; iOS support is reportedly coming ...

  7. 泽西岛2.9及更高版本中的声明式链接

    几个星期前几个月前,我正在寻找如何为Oracle Cloud项目设计新的REST API. 我计划要做的事情之一就是使用Marc Hadley在Jersey 1.x中创建的声明性链接注入. 可悲的是这 ...

  8. 泽西岛/贾克斯RS:流式JSON

    大约一年前,我写了一篇博客文章,展示了如何使用Jersey / Jax RS流式传输HTTP响应 ,最近我想做同样的事情,但是这次使用JSON. 一种常见的模式是获取我们的Java对象并获​​取该对象 ...

  9. 移动宽带客户测试软件,华为移动宽带终端检测工具(Mobile Doctor)

    MobiledoctorV2是为检测华为移动宽带终端在使用时遇到的终端问题并给出解决方法而开发的一款PC应用软件. 如何使用MobiledoctorV2? 1. 获取软件压缩包,解压软件到PC. 2. ...

最新文章

  1. Center OS 离线安装Mysql5.7
  2. 获取ip地理位置的 api接口 简介
  3. 2019.07.11
  4. Wannafly挑战赛17 - 求值2 (逆元 + 杨辉三角公式)
  5. SAP UI5 应用的 OData XML 格式的元数据请求解析原理,基于 DOMParser
  6. Newton Method in Maching Learning
  7. 跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击
  8. i红枣:在没有暴露链接地址的C#情况下
  9. ADO编程中ATL所遇到的定义问题
  10. 面试精讲之面试考点及大厂真题 - 分布式专栏 13项目中为什么要使用消息队列
  11. python if _name_==_main__如何理解Python中的if __name__ == ‘__main__’
  12. c++实现rsa算法_RSA简介
  13. 【原创】搭建spark环境中的坑及解决办法
  14. 关于字节跳动小程序授权问题解决方案
  15. FireMonkey动画进度条实现
  16. kalman滤波理解一:理论框架
  17. linux usb 从芯片,新人求教,怎么烧录Linux系统到一个小芯片上?
  18. chart.js使用学习
  19. 中医大2020年7月网考计算机应用基础,2020年7月网络教育统考《计算机应用基础》操作系统应用模拟题试卷2...
  20. 【面向对象】Java面向对象内容

热门文章

  1. redis存opc_KEPServerEX6完整免费版
  2. 通过图书编号查询python_Python图书接口调用代码实例
  3. 生物信息 python 书籍_用python做生物信息数据分析(1-环境准备)
  4. HttpClient api-连接池
  5. (转)web.xml 中的listener、 filter、servlet 加载顺序及其详解
  6. 解决eclipse中tomcat无法识别maven web项目问题
  7. 带有Prometheus的弹簧靴和Micrometer第5部分:旋转Prometheus
  8. lombok var_使用var,Lombok和Fluxtion轻松处理事件
  9. 公证服务信息_使用多个公证员提高网络吞吐量
  10. java 拼图_拼图推迟将Java 9的发布日期推迟到2017年