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



我使用的是JAX-WS Web服务生成的类,或者我们可以说是代理和短截线。我需要将该代理强制转换为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();

I am trying to mock the service in Junit
@Mock BindingProvider bp;
@Mock SomeInterface someInterface;
@Mock SomeImplService someImplService;
@Mock Map<String, Object> 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

我的问题通过使用解决了:someInterface=mock(someInterface.class,withSettings((.extraInterfaces(BindingProvider.class((;

最新更新