Spring MockRestServiceServer处理对同一URI的多个请求(自动发现)

Dan*_*zyk 17 spring integration-testing mocking spring-mvc

假设我正在为REST服务A编写Spring集成测试.该服务依次点击另一个REST服务B并获取要在REST服务C上命中的URI列表.它是一种自动发现模式.我想使用MockRestServiceServer模拟B和C响应.
现在来自B的响应是一个URI列表,它们都非常相似,为了举例说明,我的B响应是这样的:

{
    uris: ["/stuff/1.json", "/stuff/2.json", "/stuff/39.json", "/stuff/47.json"]
}
Run Code Online (Sandbox Code Playgroud)

简单地说,服务A会将它们中的每一个附加到服务C的基本URL上并发出这些请求.
模拟B很容易,因为它只有1个请求.
模拟C很麻烦,因为我必须模拟每个URI以适当的模拟响应.我想自动化它!
所以首先我写自己的匹配器来匹配不是一个完整的URL,但它的一部分:

public class RequestContainsUriMatcher implements RequestMatcher {
    private final String uri;

    public RequestContainsUriMatcher(String uri){
        this.uri = uri;
    }

    @Override
    public void match(ClientHttpRequest clientHttpRequest) throws IOException, AssertionError {
        assertTrue(clientHttpRequest.getURI().contains(uri));
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,因为我现在可以这样做:

public RequestMatcher requestContainsUri(String uri) {
    return new RequestContainsUriMatcher(uri);
}

MockRestServiceServer.createServer(restTemplate)
            .expect(requestContainsUri("/stuff"))
            .andExpect(method(HttpMethod.GET))
            .andRespond(/* I will get to response creator */);
Run Code Online (Sandbox Code Playgroud)

现在我需要的是一个响应创建者,它知道完整的请求URL以及模拟数据所在的位置(我将它作为测试资源文件夹中的json文件):

public class AutoDiscoveryCannedDataResponseCreator implements ResponseCreator {
    private final Function<String, String> cannedDataBuilder;

    public AutoDiscoveryCannedDataResponseCreator(Function<String, String> cannedDataBuilder) {
        this.cannedDataBuilder = cannedDataBuilder;
    }

    @Override
    public ClientHttpResponse createResponse(ClientHttpRequest clientHttpRequest) throws IOException {
        return withSuccess(cannedDataBuilder.apply(requestUri), MediaType.APPLICATION_JSON)
                    .createResponse(clientHttpRequest);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在很容易,我必须编写一个构建器,它将请求URI作为字符串并返回模拟数据,作为String!辉煌!

public ResponseCreator withAutoDetectedCannedData() {
    Function<String, String> cannedDataBuilder = new Function<String, String>() {
        @Override
        public String apply(String requestUri) {
            //logic to get the canned data based on URI
            return cannedData;
        }
    };

    return new AutoDiscoveryCannedDataResponseCreator(cannedDataBuilder);
}

MockRestServiceServer.createServer(restTemplate)
            .expect(requestContainsUri("/stuff"))
            .andExpect(method(HttpMethod.GET))
            .andRespond(withAutoDetectedCannedData());
Run Code Online (Sandbox Code Playgroud)

它工作正常!....对于第一个请求.
在第一个请求(/stuff/1.json)之后,我的MockRestServiceServer响应消息"断言错误:没有预期的进一步请求".
基本上,我可以向MockRestServiceServer发出尽可能多的请求,因为它有.expect()调用.因为我只有其中的一个,所以只有第一个请求会通过.
有办法解决吗?我真的不想模拟服务C 10或20次......

eme*_*ava 24

如果查看MockRestServiceServer类,它支持两个'expect()'方法.第一个默认为'ExpectedCount.once()',但第二个方法允许您更改此值

public ResponseActions expect(RequestMatcher matcher) {
    return this.expect(ExpectedCount.once(), matcher);
}

public ResponseActions expect(ExpectedCount count, RequestMatcher matcher) {
    return this.expectationManager.expectRequest(count, matcher);
}
Run Code Online (Sandbox Code Playgroud)

我发现这个票据MockRestServiceServer应该允许多次发生的期望,这概述了第二种方法的一些选项.

在你的情况下,我认为添加静态导入和使用manyTimes()方法是比for循环更简洁的代码

MockRestServiceServer
            .expect(manyTimes(), requestContainsUri("/stuff"))
            .andExpect(method(HttpMethod.GET))
Run Code Online (Sandbox Code Playgroud)

其他选择是

once();
manyTimes();
times(5);
min(2);
max(8);
between(3,6);
Run Code Online (Sandbox Code Playgroud)

  • 在4.3版本中实现 (3认同)

rap*_*oft 10

编辑:请参阅@emeraldjava的答案,它显示了Spring 4.3+用户的正确解决方案.

不幸的是,没有任何好的机制可以期待多次调用.您可以手动执行也可以使用循环,例如:

for (int i = 0; i < 10; i++) {           
        mockRestServiceServer
                .expect(requestContainsUri("/stuff"))
                .andExpect(method(HttpMethod.GET))
                .andRespond(withAutoDetectedCannedData());
}
Run Code Online (Sandbox Code Playgroud)

请注意,必须在没有任何中断的情况下调用请求,例如,不能有另一个与"/ stuff"URI不匹配的REST调用.