使用MockWebServiceServer测试Web Service客户端:请求顺序

时间:2018-08-15 13:34:37

标签: java web-services spring-ws spring-test

我需要使用spring-ws-test 3.0.1.RELEASE测试一个Web Service客户端。请求的顺序是根据业务逻辑执行的,如下所示:

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

换句话说,我的测试四次击中了模拟的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失败。

1 个答案:

答案 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个期望。

对于我引用https://docs.spring.io/spring-ws/docs/current/api/org/springframework/ws/test/client/MockWebServiceServer.html

来说,不是很明显