在模拟`BindingProvider`和生成的存根类时出现问题。

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

Problem while Mocking BindingProvider and generated stub classes

问题

  1. // 以下是翻译好的内容:
  2. // 使用JAX-WS生成的WebService类或代理和存根。我需要将该代理强制转换为BindingProvider接口,以便在运行时设置终端点。
  3. //(不幸的是,生成的代理接口不扩展BindingProvider接口)。我能够运行代码,但在JUnit中无法进行相同的模拟。
  4. // 以下是代码示例:
  5. String url = "http://<soapservice>?.wsdl";
  6. SomeInterface port = someImplService.getSomeImplPort();
  7. BindingProvider bp = (BindingProvider) port;
  8. bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
  9. port.methodcall();
  10. // 我试图在JUnit中模拟该服务:
  11. @Mock BindingProvider bp;
  12. @Mock SomeInterface someInterface;
  13. @Mock SomeImplService someImplService;
  14. @Mock Map<String, Object> context;
  15. // 第一次尝试:
  16. when(this.someImplService.getSomeImplPort()).thenReturn(bp);
  17. when(bp.getRequestContext()).thenReturn(context);
  18. when(context.put(anyObject(), anyObject())).thenReturn(context);
  19. someInterface.methodcall();
  20. // 对于上面的代码,第一行会出现编译错误,因为它期望SomeInterface的引用。
  21. // 第二次尝试:
  22. when(this.someImplService.getSomeImplPort()).thenReturn(someInterface);
  23. when((BindingProvider) someInterface).thenReturn(bp);
  24. when(bp.getRequestContext()).thenReturn(context);
  25. when(context.put(anyObject(), anyObject())).thenReturn(context);
  26. someInterface.methodcall();
  27. // 对于上面的代码,第二行会引发ClassCast异常。
  28. // 有人可以建议我如何在这种情况下模拟绑定提供程序和实现类吗?

以上是您提供的内容的翻译部分。如果您需要更多帮助,请随时提问。

英文:

> I am using JAX-WS webservice generated classes or we can say the proxy
> and stubs. I need to cast that proxy to the BindingProvider interface,
> to set endpoint at runtime. (Unfortunately the generated proxy
> interface does not extend the BindingProvider interface). I was able
> to run the code but in Junit I am not able to mock the same. Sharing
> the code below

  1. String url=&quot;http://&lt;soapservice&gt;?.wsdl&quot;
  2. SomeInterface port = someImplService.getSomeImplPort();
  3. BindingProvider bp = (BindingProvider) port;
  4. bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
  5. port.methodcall();
  6. I am trying to mock the service in Junit
  7. @Mock BindingProvider bp;
  8. @Mock SomeInterface someInterface;
  9. @Mock SomeImplService someImplService;
  10. @Mock Map&lt;String, Object&gt; context;
  11. First Try:
  12. when(this.someImplService.getSomeImplPort()).thenReturn(bp);
  13. when(bp.getRequestContext()).thenReturn(context);
  14. when(context.put(anyObject(), anyObject())).thenReturn(context);
  15. someInterface.methodcall();
  16. For above code first line compile error as it expect reference of SomeInterface
  17. Second Try:
  18. when(this.someImplService.getSomeImplPort()).thenReturn(someInterface);
  19. when((BindingProvider)someInterface).thenReturn(bp);
  20. when(bp.getRequestContext()).thenReturn(context);
  21. when(context.put(anyObject(), anyObject())).thenReturn(context);
  22. someInterface.methodcall();
  23. For above code ClassCast Exception at line 2
  24. Can someone suggest me solution how to mock the binding provider and impl classes in this case

答案1

得分: 2

我的问题得到了解决,方法是使用以下代码:

  1. someInterface = mock(SomeInterface.class, withSettings().extraInterfaces(BindingProvider.class));
英文:

My problem got solved by using:
someInterface=mock(SomeInterface.class,withSettings().extraInterfaces(BindingProvider.class));

huangapple
  • 本文由 发表于 2020年5月5日 20:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613141.html
匿名

发表评论

匿名网友

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

确定