关于模拟JAX-WS代理的Classcast
我尝试在春季(3.2.2版)中使用JaxWsPortProxyFactoryBean,但是当我尝试在单元测试中模拟端口时,它失败。类广播例外的原因是我需要将代理转换为Bindingprovider。
我试图添加withSettings()。extraInterfaces,但是我遇到了同样的异常。
https://groups.google.com/forum/?fromgroups=#!topic/mockito/YM5EF0x90_4
有没有人建议解决我的痛苦或以其他方式嘲弄港口?我添加JaxWsPortProxyFactoryBean的唯一原因是无需进行WS调用就能进行单元测试。
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);
单元测试:
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));
}
}
更新:
我认为问题不在于Bindingprovider的不同版本。
当我对代理bean进行以下更改时,它可以进行强制转换并进行ws调用。
<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>
JaxWsPortProxyFactoryBean确实实现了Bindingprovider接口,以便能够将代理转换为bindingprovider。但是当我模拟对象时,它不再是实现BindingProvider的类型。
最佳答案
经过一番研究,我发现我的单元测试看起来更像是一个集成测试。
将测试更改为以下内容以使其起作用:
@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));
}