我正在使用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));

10-08 20:01