在我的测试中,使用MockWebServiceClient
触发了Web服务。该服务对另一个Web服务执行异步回调,该回调使用MockWebServiceServer
模拟。
mockServer被调用并以指定的响应有效负载进行响应。但是,在调用verify()
时,期望没有得到验证。自定义CallbackRequestMatcher
中的错误和异常将被忽略。内置Matchers表现出相同的行为。
测试如下:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class IntegrationTest {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private WebServiceTemplate webServiceTemplate;
private MockWebServiceServer mockServer;
private MockWebServiceClient mockWebServiceClient;
@Before
public void setUp() {
mockServer = MockWebServiceServer.createServer(webServiceTemplate);
mockWebServiceClient = MockWebServiceClient.createClient(applicationContext);
}
@Test
public void integrationTest() throws InterruptedException {
mockServer.expect(new CallbackRequestMatcher()).andRespond(withPayload(getXML()));
mockWebServiceClient.sendRequest(withSoapEnvelope(getSomeOtherXML()
.andExpect(new MessageIdMatcher())
.andExpect(new UpdateLifecycleMatcher());
// Wait for asynchronous call
Thread.sleep(1000);
mockServer.verify();
}}
自定义RequestMatcher看起来像这样
public class CallbackRequestMatcher implements RequestMatcher {
@Override
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
try {
SOAPBody soapBody = ((SaajSoapMessage) request).getSaajMessage().getSOAPBody();
Assert.assertEquals("foo", soapBody.getFirstChild().getLocalName());
} catch (SOAPException e) {
e.printStackTrace();
}
}}
spring-ws-test 2.4.0.RELEASE版本已使用,但如有必要可以升级。
如果RequestMatcher引发错误,为什么模拟服务器不会失败?