如何使用Spring WS Test测试SOAPAction头

时间:2013-12-24 10:13:30

标签: soap soap-client spring-ws

我的应用程序使用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
    }

}

所以我要测试的是所谓的soap动作。所以我想在我的测试类中使用这样的东西:

mockServer.expect(....withSoapAction("calledSoapAction")).andRespond(...

1 个答案:

答案 0 :(得分:3)

创建自己的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));
    }
}

用法

mockServer.expect(connectionTo("http://server/"))
        .andExpect(new SoapActionMatcher("calledSoapAction"))
        .andRespond(withPayload(...)));
相关问题