【发布时间】:2021-08-25 19:48:40
【问题描述】:
我在使用 Spring 和 Wiremock 进行集成测试时遇到了一个非常奇怪的情况:突然,一个特定的测试开始间歇性地失败。以下错误的sn-p:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:10314/my/endpoint": Software caused connection abort: recv failed; nested exception is java.net.SocketException: Software caused connection abort: recv failed
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785) ~[spring-web-5.3.7.jar:5.3.7]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711) ~[spring-web-5.3.7.jar:5.3.7]
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:468) ~[spring-web-5.3.7.jar:5.3.7]
... more logs here ...
上下文如下: 我添加了一个使用wiremock 来存根响应的新测试:
wireMockServer.stubFor(WireMock.post("/my/endpoint")
.withRequestBody(containing(aJsonRequestBodyHere))
.willReturn(aResponse()
.withBody(aJsonResponseHere)
.withStatus(HttpStatus.OK.value())
.withHeader(HttpHeader.CONTENT_TYPE.toString(), CONTENT_TYPE_APPLICATION_JSON)));
对该存根端点的调用如下:
given()
.when()
.get("my/endpoint")
.then()
.body(containsString(theExpectedJsonResponse)))
.statusCode(200);
奇怪的部分:
- 在我的本地机器上运行相同的测试没有任何问题 - 如果单独运行
- 在我的机器上运行所有测试时,有时相同的测试会失败,有时则不会
- 只有这个测试每次都失败;没有其他测试失败
- 在 Jenkins 上运行测试时,100% 失败
【问题讨论】:
标签: spring integration-testing wiremock