【问题标题】:How to test SOAPAction header with Spring WS Test如何使用 Spring WS Test 测试 SOAPAction 标头
【发布时间】:2013-12-24 10:13:30
【问题描述】:

我的应用正在使用 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
    }

}

所以我要测试的是调用的肥皂动作。所以我想要在我的测试课中这样的东西:

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

【问题讨论】:

  • 你能解释一下你的测试场景吗?
  • 我加了一点sn-p,希望更清楚。

标签: soap soap-client spring-ws


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2017-09-01
    • 2012-02-19
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多