关于模拟JAX-WS代理的Classcastexception

时间:2013-05-15 08:56:10

标签: java spring mocking jax-ws mockito

模拟JAX-WS代理的分类

我尝试在Spring(3.2.2发行版)中使用JaxWsPortProxyFactoryBean,但是当我尝试在单元测试中模拟端口时它失败了。 classcast异常的原因是我需要将代理转换为Bindingprovider。

我尝试添加withSettings()。extraInterfaces,但我得到了同样的异常。 https://groups.google.com/forum/?fromgroups=#!topic/mockito/YM5EF0x90_4

有没有人建议解决我在屁股上的痛苦或以其他方式嘲笑港口?我添加JaxWsPortProxyFactoryBean的唯一原因是能够在不进行WS调用的情况下进行单元测试。

的ApplicationContext豆:

<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的类型。

2 个答案:

答案 0 :(得分:0)

经过一番研究后,我发现我的单元测试看起来更像是集成测试..

将测试更改为以下内容以使其正常工作:

 @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));
    }

答案 1 :(得分:0)

这是我尝试和工作的。

  1. 创建抽象类/接口

    public interface AbstractPZGetEmployeeDataV3 extends PZGetEmployeeDataV3Port, BindingProvider {
    }
    
  2. 用Inject模拟了上面的内容

    @Spy
    AbstractPZGetEmployeeDataV3 spyPort;
    
  3. 向其中添加更多肉

    Mockito.lenient().when(pZGetEmployeeDataV3_Service.getPZGetEmployeeDataV3SOAPAPIGEEDEV()).thenReturn(spyPort);
    GetAllGenericEmployeeDataResponse soapResp = new GetAllGenericEmployeeDataResponse();
    soapResp.setEmploymentStatusCode("S");
    soapResp.setLastName("LastNAme");
    Mockito.lenient().when(spyPort.getAllGenericEmployeeData(Mockito.any())).thenReturn(soapResp);