使用 MockWebServiceServer 测试 Web 服务客户端:请求序列

Testing Web Service client with MockWebServiceServer: sequence of requests

我需要使用 spring-ws-test 3.0.1.RELEASE 测试 Web 服务客户端。按照业务逻辑执行请求顺序如下:

  1. 向路由器端点询问特定商店的 URL;
  2. 点击特定商店的 URL 以获得员工的许可来做一些员工;
  3. 向路由器端点询问同一商店的 URL(有具体原因重复请求);
  4. 打几乎都是同店的URL做一些员工代工。

换句话说,我的测试命中了一个模拟的 Web 服务服务器四次。它两次以相同的有效载荷命中路由器端点 (1, 3),两次以不同的有效载荷命中特定商店的 URL (2, 4)。

我想知道我应该如何编写模拟 Web 服务服务器的代码来承受这样的负载?考虑到:

@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 失败。

问题部分揭示了答案。由于我有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 个期望。

参考 https://docs.spring.io/spring-ws/docs/current/api/org/springframework/ws/test/client/MockWebServiceServer.html

对我来说不是很明显