Apache CXF 将 xml 转换为用于单元测试的 SoapMessage

Apache CXF transform xml to SoapMessage for Unit-Tests

我想为 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: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?

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

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

因此createSoapMessage方法是可以实现的,但是没有想的那么简单。 我将非常感谢更简单的解决方案。

目前,我使用 SoapUI 测试拦截器。