我需要使用spring-ws-test 3.0.1.RELEASE测试一个Web Service客户端。请求的顺序是根据业务逻辑执行的,如下所示:
换句话说,我的测试四次击中了模拟的Web服务服务器。它两次以相同的有效负载命中路由器端点(1、3),两次以特定的负载命中特定商店的URL(2、4)。
我想知道我应该如何编码模拟的Web Service服务器以承受这样的有效负载?考虑到:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeClientTest {
@Autowired
private SomeClient someClient;
private MockWebServiceServer mockWebServiceServer;
@Before
public void setUp() {
mockWebServiceServer = MockWebServiceServer.createServer(someClient);
}
@Test
public void fourHitsMethodTest() {
//mocks the 1st and the 3rd responses
mockWebServiceServer
.expect(payload(getUrlRequest()))
.andRespond(withPayload(getUrlResponse()));
//mocks the 2nd response
mockWebServiceServer
.expect(payload(getEmployeesPermissionRequest()))
.andRespond(withPayload(getEmployeesPermissionResponse()));
//mocks the 4th response
mockWebServiceServer
.expect(payload(doSomeStaffRequest()))
.andRespond(withPayload(doSomeStaffResponse()));
//those 4 hits are been performed in fourHitsMethod()
assertNotNull(someClient.fourHitsMethod("foo", "bar"));
mockWebServiceServer.verify();
}
}
java.lang.AssertionError: No further connections expected
失败。
答案 0 :(得分:2)
该问题部分地表明了答案。由于我有4个点击,因此我必须适当地宣布4个期望。而且我希望他们能被打来。 MockWebServiceServer
会在达到期望后删除所有期望。以下工作正常:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeClientTest {
@Autowired
private SomeClient someClient;
private MockWebServiceServer mockWebServiceServer;
@Before
public void setUp() {
mockWebServiceServer = MockWebServiceServer.createServer(someClient);
}
@Test
public void fourHitsMethodTest() {
//mocks only the 1st, but not the 3rd response
mockWebServiceServer
.expect(payload(getUrlRequest()))
.andRespond(withPayload(getUrlResponse()));
//mocks the 2nd response
mockWebServiceServer
.expect(payload(getEmployeesPermissionRequest()))
.andRespond(withPayload(getEmployeesPermissionResponse()));
//mocks the 3rd response
mockWebServiceServer
.expect(payload(getUrlRequest()))
.andRespond(withPayload(getUrlResponse()));
//mocks the 4th response
mockWebServiceServer
.expect(payload(doSomeStaffRequest()))
.andRespond(withPayload(doSomeStaffResponse()));
//those 4 hits are been performed in fourHitsMethod()
assertNotNull(someClient.fourHitsMethod("foo", "bar"));
mockWebServiceServer.verify();
}
}
请注意与问题相比期望值的差异。在这里,我对问题中的3个有4个期望。
来说,不是很明显