【问题标题】:SoapFault handling with Spring WS client - WebServiceGatewaySupport and WebServiceTemplate使用 Spring WS 客户端处理 SoapFault - WebServiceGatewaySupport 和 WebServiceTemplate
【发布时间】:2017-03-28 19:57:54
【问题描述】:

我正在尝试使用 WebServiceGatewaySupport 编写 Spring WS 客户端。我设法测试客户端是否成功请求和响应。现在我想为肥皂故障编写测试用例。

public class MyClient extends WebServiceGatewaySupport {
     public ServiceResponse method(ServiceRequest serviceRequest) {
          return (ServiceResponse) getWebServiceTemplate().marshalSendAndReceive(serviceRequest);
}

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringTestConfig.class)
@DirtiesContext 
public class MyClientTest {
   @Autowired
   private MyClient myClient;

   private MockWebServiceServer mockServer;

   @Before
    public void createServer() throws Exception {
        mockServer = MockWebServiceServer.createServer(myClient);
    }
}

我的问题是如何在模拟服务器中存根肥皂故障响应,以便我的自定义 FaultMessageResolver 能够解组肥皂故障?

我在下面尝试了几件事,但没有任何效果。

 // responsePayload being SoapFault wrapped in SoapEnvelope
 mockServer.expect(payload(requestPayload))
            .andRespond(withSoapEnvelope(responsePayload));

 // tried to build error message
 mockServer.expect(payload(requestPayload))
            .andRespond(withError("soap fault string"));

 // tried with Exception
 mockServer.expect(payload(requestPayload))
            .andRespond(withException(new RuntimeException));

感谢任何帮助。谢谢!

跟进:

好的,通过SoapEnvelope(payload) 我设法让控制器转到我的自定义MySoapFaultMessageResolver。

public class MyCustomSoapFaultMessageResolver implements FaultMessageResolver     {

private Jaxb2Marshaller jaxb2Marshaller;

@Override
public void resolveFault(WebServiceMessage message) throws IOException {

    if (message instanceof SoapMessage) {
        SoapMessage soapMessage = (SoapMessage) message;

        SoapFaultDetailElement soapFaultDetailElement = (SoapFaultDetailElement) soapMessage.getSoapBody()
            .getFault()
            .getFaultDetail()
            .getDetailEntries()
            .next();
        Source source = soapFaultDetailElement.getSource();
        jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath("com.company.project.schema");
        Object object = jaxb2Marshaller.unmarshal(source);
        if (object instanceof CustomerAlreadyExistsFault) {
            throw new CustomerAlreadyExistsException(soapMessage);
        }
    }
  }
} 

但是说真的!!!我必须解组每条消息并检查它的实例。作为客户,我应该彻底处理此处服务的所有可能异常,并创建自定义运行时异常并将其从解析器中抛出。最后,它被 WebServiceTemplate 捕获并作为运行时异常重新抛出。

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • 不是真的!我最终使用了不同的客户端
  • 查看我的答案。也许这就是解决方案。

标签: integration-testing soap-client spring-ws


【解决方案1】:

你可以试试这样的:

@Test
public void yourTestMethod() // with no throw here
{
    Source requestPayload = new StringSource("<your request>");
    String errorMessage = "Your error message from WS";

    mockWebServiceServer
        .expect(payload(requestPayload))
        .andRespond(withError(errorMessage));

    YourRequestClass request = new YourRequestClass();
    // TODO: set request properties...

    try {
        yourClient.callMethod(request);    
    } 
    catch (Exception e) {
        assertThat(e.getMessage()).isEqualTo(errorMessage);
    }
    mockWebServiceServer.verify();
}

在这部分代码中,mockWebServiceServer代表MockWebServiceServer类的实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多