gpt4 book ai didi

java - 模拟 BindingProvider 和生成的 stub 类时出现问题

转载 作者:行者123 更新时间:2023-12-01 16:54:35 24 4
gpt4 key购买 nike

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="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));

关于java - 模拟 BindingProvider 和生成的 stub 类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61613141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com