Classcast on mocking JAX-WS proxy
I try to use the JaxWsPortProxyFactoryBean in Spring (3.2.2 Release), but it fails when I try to mock the port in my unit test. The reason for the classcast exception is that I need to cast the proxy to a Bindingprovider.
I tried to add the withSettings().extraInterfaces, but I get the same exception. https://groups.google.com/forum/?fromgroups=#!topic/mockito/YM5EF0x90_4
Has anyone any suggestions to resolve my pain in the ass or doing the mocking of the port in some other way? The only reason I added the JaxWsPortProxyFactoryBean was to be able to unit test without doing the WS call.
ApplicationContext-beans:
<bean id="port" class="org.mockito.Mockito" factory-method="mock" >
<constructor-arg index="0" value="name of the portType class" />
</bean>
WSHelper:
import com.sun.xml.ws.client.BindingProviderProperties;
import javax.xml.ws.BindingProvider;
@Resource
private ThePortTypeClass port;
…
BindingProvider bp = (BindingProvider) port;
...
Portfolioimpl:
REQUEST request= RequestHelper.getRequest(vo);
ThePortTypeClass port =wSHelper.getPort();
RESPONSE response = null;
response = port.wSMethod(request);
unit test:
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml"})
public class PortfolieTest {
@Resource
private ThePortTypeClass port;
@Resource
private WSHelper wsHelper;
@Autowired
Portfolio portfolio;
@Before
public void setup(){
RESPONSE response = new RESPONSE();
…
port=mock(ThePortTypeClass.class, withSettings().extraInterfaces(BindingProvider.class));
when(port.method(any(REQUEST.class))).thenReturn(response);
}
@Test
public void update() {
assertEquals(true, portfolio.update(someParameters));
}
}
Update:
I don't think the issue is different versions of the Bindingprovider. When I do the following change to the proxy bean it's able to do the cast and does the ws call.
<bean id="port" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="ThePortTypeClass" />
<property name="namespaceUri" value="nm" />
<property name="serviceName" value="theServicename" />
<property name="endpointAddress" value="theUrl" />
<property name="wsdlDocumentUrl" value="theUrl" />
</bean>
The JaxWsPortProxyFactoryBean does implements the Bindingprovider interface so that it's able to cast the proxy to a bindingprovider. But when I mock the object it is no longer of a type that implements the BindingProvider.
After some research I found out my unit test looked more like a integration test..
Changed the test to the following to make it work:
@RunWith(MockitoJUnitRunner.class)
public class PortfolieTest {
@InjectMocks
Portfolio portfolio = new PortfolioImpl();
@Mock
private thePortType port;
@Mock
private WSHelper wsHelper = new WSHelper();
@Before
public void setup() throws Feil, MalformedURLException, DocumentException {
RESPONSE response = new RESPONSE();
…
when(wsHelper.gePort(someParameters).thenReturn(port);
when(port.wsmethod(any(REQUEST.class))).thenReturn(response);
}
@Test
public void update() throws MalformedURLException, DocumentException {
assertEquals(true, portfolio.update(someParameters));
}