我的应用程序正在使用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(...
创建
自己的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(...)));