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

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

Problem while Mocking BindingProvider and generated stub classes

问题

// 以下是翻译好的内容:

// 使用JAX-WS生成的WebService类或代理和存根。我需要将该代理强制转换为BindingProvider接口,以便在运行时设置终端点。
//(不幸的是,生成的代理接口不扩展BindingProvider接口)。我能够运行代码,但在JUnit中无法进行相同的模拟。
// 以下是代码示例:

String url = "http://<soapservice>?.wsdl";
SomeInterface port = someImplService.getSomeImplPort();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
port.methodcall();

// 我试图在JUnit中模拟该服务:

@Mock BindingProvider bp;
@Mock SomeInterface someInterface;
@Mock SomeImplService someImplService;
@Mock Map<String, Object> context;

// 第一次尝试:
when(this.someImplService.getSomeImplPort()).thenReturn(bp);
when(bp.getRequestContext()).thenReturn(context);
when(context.put(anyObject(), anyObject())).thenReturn(context);
someInterface.methodcall();

// 对于上面的代码,第一行会出现编译错误,因为它期望SomeInterface的引用。

// 第二次尝试:
when(this.someImplService.getSomeImplPort()).thenReturn(someInterface);
when((BindingProvider) someInterface).thenReturn(bp);
when(bp.getRequestContext()).thenReturn(context);
when(context.put(anyObject(), anyObject())).thenReturn(context);
someInterface.methodcall();

// 对于上面的代码,第二行会引发ClassCast异常。

// 有人可以建议我如何在这种情况下模拟绑定提供程序和实现类吗?

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

英文:

> 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

  String url=&quot;http://&lt;soapservice&gt;?.wsdl&quot;
  SomeInterface port = someImplService.getSomeImplPort();
  BindingProvider bp = (BindingProvider) port;
  bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
  port.methodcall();
  
  
  I am trying to mock the service in Junit
  
  @Mock BindingProvider bp;
  @Mock SomeInterface someInterface;
  @Mock SomeImplService someImplService;
  @Mock Map&lt;String, Object&gt; context;
   
   First Try:
   when(this.someImplService.getSomeImplPort()).thenReturn(bp);
   when(bp.getRequestContext()).thenReturn(context);
   when(context.put(anyObject(), anyObject())).thenReturn(context);
   someInterface.methodcall();
   
   For above code first line compile error as it expect reference of SomeInterface
   
   Second Try:
   when(this.someImplService.getSomeImplPort()).thenReturn(someInterface);
   when((BindingProvider)someInterface).thenReturn(bp);
   when(bp.getRequestContext()).thenReturn(context);
   when(context.put(anyObject(), anyObject())).thenReturn(context);
   someInterface.methodcall();
   
   For above code ClassCast Exception at line 2
   
   
   Can someone suggest me solution how to mock the binding provider and impl classes in this case

答案1

得分: 2

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

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:

确定