如何使用 Spring WS Test 测试 SOAPAction 标头

Mat*_*att 2 soap spring-ws soap-client

我的应用程序正在使用 spring-ws 的 WebServiceTemplate 调用外部 Soap WS,我在测试中使用 MockWebServiceServer 进行模拟。

它可以很好地根据请求有效负载模拟响应。

但是现在我想测试调用了哪个 SOAP 操作。它应该在请求的“SOAPAction”HTTP 标头中定义。

我正在使用 Spring-WS 2.1.4。

有谁知道是否可以测试它以及如何测试?

这是我的测试课:

public class MyWebServiceTest {
    @Autowired
    private WebServiceTemplate webServiceTemplate;

    private MockWebServiceServer mockServer;                                               

    @Before
    public void createServer() throws Exception {
        mockServer = MockWebServiceServer.createServer(webServiceTemplate);
    }

    @Test
    public void callStambiaWithExistingFileShouldSuccess() throws IOException {

        Resource requestPayload = new ClassPathResource("request-payload.xml");
        Resource responseSoapEnvelope = new ClassPathResource("success-response-soap-envoloppe.xml");

        mockServer.expect(payload(requestPayload)).andRespond(withSoapEnvelope(responseSoapEnvelope));
        //init job
        //myService call the webservice via WebServiceTemplate
        myService.executeJob(job);

        mockServer.verify();
        //some asserts
    }

}
Run Code Online (Sandbox Code Playgroud)

所以我要测试的是调用的soap动作。所以我想在我的测试课上做这样的事情:

mockServer.expect(....withSoapAction("calledSoapAction")).andRespond(...
Run Code Online (Sandbox Code Playgroud)

ben*_*y23 5

创建自己RequestMatcher的非常简单:

public class SoapActionMatcher implements RequestMatcher {

    private final String expectedSoapAction;

    public SoapActionMatcher(String expectedSoapAction) {
        this.expectedSoapAction = SoapUtils.escapeAction(expectedSoapAction);
    }

    @Override
    public void match(URI uri, WebServiceMessage request) 
            throws IOException, AssertionError {
        assertThat(request, instanceOf(SoapMessage.class));
        SoapMessage soapMessage = (SoapMessage) request;
        assertThat(soapMessage.getSoapAction(), equalTo(expectedSoapAction));
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

mockServer.expect(connectionTo("http://server/"))
        .andExpect(new SoapActionMatcher("calledSoapAction"))
        .andRespond(withPayload(...)));
Run Code Online (Sandbox Code Playgroud)