Spring 使用“withSoapEnvelope”创建的 WS 测试 MockWebServiceServer 响应为空

Spring WS Test MockWebServiceServer response created with `withSoapEnvelope` is empty

我想用模拟服务器测试 SOAP 客户端。

如果我使用 mockServer.expect(soapEnvelope(request)).andRespond(withPayload(response)) 一切都按预期工作。

但我想要的是用包含 SOAP 中相关信息的特定 SOAP 消息进行响应 header:mockServer.expect(soapEnvelope(request)).andRespond(withSoapEnvelope(response))

如果我这样做,soap 消息的 header 和 body 为空,因此我的测试失败。谁能body告诉我问题是什么?

@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>1234567.0</ns11:zemisNumber>
            <ns11:firstName>Peter</ns11:firstName>
            <ns11:lastName>Muster</ns11:lastName>
            <ns11:gender>2</ns11:gender>
        </ns11:GetPersonDetailsResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

遇到同样的问题,通过提供简单的自定义 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")));