Apache CXF将xml转换为SoapMessage以进行单元测试

时间:2018-10-30 17:04:08

标签: unit-testing soap cxf interceptor

我想为AbstractSoapInterceptor创建一个小的单元测试,如下所示:

public class SimpleInterceptorTest {

    private SimpleInterceptor simpleInterceptor;

    @BeforeMethod
    public void setUp() {
        simpleInterceptor = new SimpleInterceptor();
    }

    @Test
    public void testHandleMessage() throws Exception {
        SoapMessage soapMessage = createSoapMessage("requests/sampleRequest.xml");
        simpleInterceptor.handleMessage(soapMessage);

        // Assertions
    }
}

requests/sampleRequest.xml是一个Soap-Request,例如:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <soap:Header>
        <wsse:Security>
            <wsc:SecurityContextToken xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="sct">
                <wsc:Identifier>abcd</wsc:Identifier>
            </wsc:SecurityContextToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <wst:RequestSecurityToken xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" >
            <wst:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</wst:TokenType>
            <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Validate</wst:RequestType>
            <wst:ValidateTarget>
                <wsse:SecurityTokenReference>
                    <wsse:Reference URI="#sct"/>
                </wsse:SecurityTokenReference>
            </wst:ValidateTarget>
        </wst:RequestSecurityToken>
    </soap:Body>
</soap:Envelope>

现在我有以下问题:

  1. 这是测试拦截器的正确方法吗?

  2. 如果是,如何实施 方法createSoapMessage

1 个答案:

答案 0 :(得分:1)

以下是Apache拦截器的单元测试的示例:

https://github.com/apache/cxf/blob/master/rt/wsdl/src/test/java/org/apache/cxf/wsdl/interceptors/DocLiteralInInterceptorTest.java

因此,可以实现createSoapMessage方法,但是并没有所希望的那么简单。 非常感谢您提供更简单的解决方案。

现在,我用SoapUI测试拦截器。