我想用模拟服务器测试SOAP客户端。
如果我使用mockServer.expect(soapEnvelope(request)).andRespond(withPayload(response))
,一切正常。
但是我想要用特定的SOAP消息响应,该消息在SOAP标头中包含相关信息:mockServer.expect(soapEnvelope(request)).andRespond(withSoapEnvelope(response))
如果这样做,soap消息的标题和正文为空,因此我的测试失败。谁能告诉我问题出在哪里?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MigekSoapZemisSearchConfiguration.class }, initializers = {
ConfigFileApplicationContextInitializer.class })
@DirtiesContext
@ActiveProfiles("test")
public class MigekSoapZemisSearchClientTest {
@Autowired
private ApplicationContext appContext;
@Autowired
private MigekSoapZemisSearchClient client;
private MockWebServiceServer mockServer;
@Before
public void init() {
mockServer = MockWebServiceServer.createServer(client);
}
@Test
public void valid_xsd_request_response_test() throws IOException {
Resource request = appContext.getResource("classpath:soap/soapRequest.xml");
RequestMatcher rm = soapEnvelope(request);
Resource response = appContext.getResource("classpath:soap/soapResponse.xml");
ResponseCreator respce = withSoapEnvelope(response);
mockServer.expect(rm).andRespond(respce);
JAXBElement<PersonType> result = client.getPersonDetails("35db14e0-5237-435e-b2a6-02f1e75fa489");
Assert.assertNotNull(result);
Assert.assertEquals(result.getValue().getFirstName(), "Marina");
mockServer.verify();
}
}
soapResponse.xml:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns10:ResponseHeader
xmlns:ns10="http://ejpd.admin.ch/bfm/zemis/header/types/v1"
xmlns="http://www.ech.ch/xmlns/eCH-0135/1">
<ns10:MessageId>35db14e0-5237-435e-b2a6-02f1e75fa489</ns10:MessageId>
<ns10:Notice Severity="WARNING">
<ns10:Source>getPersonDetails</ns10:Source>
<ns10:Code>7</ns10:Code>
<ns10:Message>Resultat beinhaltet aufgrund fehlender Berechtigung
nicht alle angeforderten Elemente(PersonType.ResidencePermit)
</ns10:Message>
</ns10:Notice>
</ns10:ResponseHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns11:GetPersonDetailsResponse
xmlns:ns11="http://ejpd.admin.ch/bfm/zemis/personsearchservice/types/v5">
<ns11:zemisNumber>1550253.7</ns11:zemisNumber>
<ns11:firstName>Marina</ns11:firstName>
<ns11:lastName>Zikiċ</ns11:lastName>
<ns11:gender>2</ns11:gender>
</ns11:GetPersonDetailsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
答案 0 :(得分:1)
有同样的问题,可以通过提供以下代码段的简单自定义ResponseCreator实现来解决。
static class ResourceBasedResponseCreator implements org.springframework.ws.test.client.ResponseCreator {
private final Resource responseResource;
public ResourceBasedResponseCreator(Resource responseResource) {
Assert.notNull(responseResource, "Response resource must be provided");
Assert.isTrue(responseResource.exists(), "Response resource not exists");
this.responseResource = responseResource;
}
@Override
public WebServiceMessage createResponse(URI uri, WebServiceMessage request, WebServiceMessageFactory messageFactory) throws IOException {
return messageFactory.createWebServiceMessage(responseResource.getInputStream());
}
}
并在测试初始化时使用它
mockServer.expect(connectionTo(proxiedClient.getDefaultUri()))
.andExpect(soapEnvelope(resourceLoader.getResource("path/to/SavedSoapRequest.xml")))
.andRespond(new ResourceBasedResponseCreator(resourceLoader.getResource("path/to/SavedSoapResponse.xml")));