gpt4 book ai didi

spring - MockRestServiceServer 未正确验证请求

转载 作者:行者123 更新时间:2023-11-28 19:53:34 26 4
gpt4 key购买 nike

我正在尝试为我的 spring 集成流程编写集成测试。我想用MockRestServiceServer 记录传出请求(使用 http:outbound-gateway)并将其匹配到 Rest 服务器。但是,当我调用 mockServer 的验证方法时,它没有按预期进行验证。

我正在按以下方式编写我的测试:

RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

mockServer.expect(requestTo("adfasfadf.com")).andExpect(method(HttpMethod.GET));

// Call spring integration flow here

mockServer.verify();

当我检查 MockRestServiceServer 的验证方法时,它没有调用 RequestMatchers 的匹配方法,我认为这个逻辑有问题。我在这里遗漏了什么吗?

/**
* Verify that all expected requests set up via
* {@link #expect(RequestMatcher)} were indeed performed.
* @throws AssertionError when some expectations were not met
*/
public void verify() {
if (this.expectedRequests.isEmpty() || this.expectedRequests.equals(this.actualRequests)) {
return;
}
throw new AssertionError(getVerifyMessage());
}

最佳答案

经过几个小时的调试,我意识到 MockRestServiceServer 在执行请求期间运行匹配器。所以,如果你有一个围绕请求执行的异常处理程序,你的断言永远不会被正确断言。此代码来自运行匹配器的 RequestMatcherClientHttpRequest

@Override
public ClientHttpResponse executeInternal() throws IOException {
if (this.requestMatchers.isEmpty()) {
throw new AssertionError("No request expectations to execute");
}
if (this.responseCreator == null) {
throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, "
+ "e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())");
}
for (RequestMatcher requestMatcher : this.requestMatchers) {
requestMatcher.match(this);
}
setResponse(this.responseCreator.createResponse(this));

return super.executeInternal();
}

我认为这应该被视为一个错误,因为我认为断言必须在应用程序执行后执行。

关于spring - MockRestServiceServer 未正确验证请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33354960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com