Junit做单元测试时如何实例化一个远程的服务

planitary 2018-01-23 04:58:29
我在编写公司项目的测试用例的时候,碰到这种情况:
在一个类的方法中有一个参数需要通过启动一个很复杂的服务从而获取到,
类似于:
我有一个测试用例ADao——>我需要将这个测试用例注入到测试类B的入口中B.b(Adao)——>但B的类需要调用一个类C中的方法c,这个方法c中有一个参数value需要启动一个远程的fienclient服务才能得到——>String value = fienclient.getCode("msg");
但是在实际执行测试的时候,是不可能去一步步启动这些服务从来获取这个参数的值的,所以我想知道有没有mock之类的方法模拟一个值来替代实际的值的从而不让用例来跑真正的服务,或者是在不启动服务的情况下来获取这个服务中的实例呢?

PS:报错信息
java.lang.IllegalArgumentException: instance can not be null

at org.springframework.util.Assert.notNull(Assert.java:134)
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.reconstructURI(RibbonLoadBalancerClient.java:48)
at io.github.jhipster.security.uaa.LoadBalancedResourceDetails.getAccessTokenUri(LoadBalancedResourceDetails.java:48)
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.getAccessTokenUri(OAuth2AccessTokenSupport.java:156)
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:137)
at org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider.obtainAccessToken(ClientCredentialsAccessTokenProvider.java:44)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:148)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:121)
at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.acquireAccessToken(OAuth2FeignRequestInterceptor.java:171)
at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.getToken(OAuth2FeignRequestInterceptor.java:127)
at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.extract(OAuth2FeignRequestInterceptor.java:112)
at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.apply(OAuth2FeignRequestInterceptor.java:100)
at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:158)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:88)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy190.getNextSequenceGenerateCode(Unknown Source)
at com.kirincs.fund.bookkeeper.service.impl.VoucherServiceImpl.save(VoucherServiceImpl.java:33)

定位到项目中就是我说的那个情况。
...全文
831 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
planitary 2018-01-24
  • 打赏
  • 举报
回复
引用 2 楼 aaa12365 的回复:
使用jmockit来mock外部服务 示例: @Test public void testGetCanUseAmtNoDSUseExpectations() throws RuleException { //mock 外部dubbo服务 new Expectations(aicaiUserFacade) { { aicaiUserFacade.getAccountDetail(12587739l, AccountTypeEnum.CREDIT.getType()); UserAccountGetDetailVo re1 = new UserAccountGetDetailVo(); re1.setAccountId(1111l); re1.setStatus("0");//根据业务逻辑需要是否要设置mock返回的值 re1.setAmtBalaval(100l); result = re1; aicaiUserFacade.getAccountDetail(12587739l, AccountTypeEnum.ACCOUNT.getType()); UserAccountGetDetailVo re2 = new UserAccountGetDetailVo(); re2.setAccountId(2222l); re2.setStatus("0");//根据业务逻辑需要是否要设置mock返回的值 re2.setAmtBalaval(200l); result = re2; } }; //执行需要测试的方法 long amt = tradeFacade.getCanUseAmtNoDS(12587739l); //可以校验mock方法的执行逻辑 new Verifications() { { aicaiUserFacade.getAccountDetail(anyLong, anyString); times = 2; } }; //必须要有断言,判断要测试的方法是否正确 Assert.assertEquals(200l, amt); }
您好,这个方法是直接mock掉外部服务所在的整个类么?还是仅仅是mock了外部服务所在的方法?
aaa12365 2018-01-23
  • 打赏
  • 举报
回复
使用jmockit来mock外部服务 示例: @Test public void testGetCanUseAmtNoDSUseExpectations() throws RuleException { //mock 外部dubbo服务 new Expectations(aicaiUserFacade) { { aicaiUserFacade.getAccountDetail(12587739l, AccountTypeEnum.CREDIT.getType()); UserAccountGetDetailVo re1 = new UserAccountGetDetailVo(); re1.setAccountId(1111l); re1.setStatus("0");//根据业务逻辑需要是否要设置mock返回的值 re1.setAmtBalaval(100l); result = re1; aicaiUserFacade.getAccountDetail(12587739l, AccountTypeEnum.ACCOUNT.getType()); UserAccountGetDetailVo re2 = new UserAccountGetDetailVo(); re2.setAccountId(2222l); re2.setStatus("0");//根据业务逻辑需要是否要设置mock返回的值 re2.setAmtBalaval(200l); result = re2; } }; //执行需要测试的方法 long amt = tradeFacade.getCanUseAmtNoDS(12587739l); //可以校验mock方法的执行逻辑 new Verifications() { { aicaiUserFacade.getAccountDetail(anyLong, anyString); times = 2; } }; //必须要有断言,判断要测试的方法是否正确 Assert.assertEquals(200l, amt); }
planitary 2018-01-23
  • 打赏
  • 举报
回复
才发现有个单词打错了,不是feinclient应该是feignclient才对

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧