2

关于模拟 JAX-WS 代理的 Classcast

我尝试在 Spring(3.2.2 版本)中使用 JaxWsPortProxyFactoryBean,但是当我尝试在单元测试中模拟端口时它失败了。classcast 异常的原因是我需要将代理强制转换为 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>

WSH 助手:

    import com.sun.xml.ws.client.BindingProviderProperties;
    import javax.xml.ws.BindingProvider;

       @Resource
        private  ThePortTypeClass port;
    …

       BindingProvider bp = (BindingProvider) port; 
...

投资组合:

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

4

2 回答 2

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));
    }
于 2013-05-15T13:40:33.973 回答
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);
    
于 2020-08-24T19:18:22.033 回答