我正在使用Wiremock模拟集成测试中的外部操作员响应:
@ClassRule
public static WireMockClassRule zainWireMockStatic = new WireMockClassRule(9900);
并获得此异常
requesting to java.net.SocketTimeoutException: connect timed out timed out
这是我的窃听器
private static void wireMockZainUnSubscriptionRequest() {
zainWireMockStatic.stubFor(get(urlPathMatching("/api/unsubscribe")).willReturn(
aResponse().withStatus(200).withHeader("Content-Type", "application/json")
.withBody(FileUtils.readFileFromClasspath(
"data/mocks/zain_unsubscribe_success_response.json"))));
}
这是我的测试
@Test
public void unsubscribeUserWithSuccessResponse() {
wireMockZainUnSubscriptionRequest();
given().body(FileUtils.readFileFromClasspath("data/message/unsubscribe_request.json"))
.contentType(ContentType.JSON).post(UNSUBSCRIBE_API).then().statusCode(200)
.body("user_id", equalTo(USER_ID));
}
答案 0 :(得分:1)
嗨,我遇到了同样的问题,我遵循了bernyfox on the github的建议并解决了问题。
我的问题是http客户端报告了SocketTimeout。提议的解决方案是告诉httpclient连接将以扩展名关闭。
我做了这个:
public class ConnectionCloseExtension extends ResponseTransformer {
@Override
public ResponseDefinition transform(Request request, ResponseDefinition response, FileSource files) {
HttpHeaders headersWithClose;
HttpHeader closeHeader = new HttpHeader("Connection", "Close");
if(response.getHeaders()!=null){
headersWithClose = HttpHeaders.copyOf(response.getHeaders())
.plus(closeHeader);
}else{
headersWithClose = new HttpHeaders(closeHeader);
}
response.setHeaders(headersWithClose);
return response;
}
@Override
public String name() {
return "ConnectionCloseExtension";
} }
并应用它:
@Rule
public WireMockRule wireMockServer = new WireMockRule(wireMockConfig()
.extensions(new ConnectionCloseExtension()));
答案 1 :(得分:0)
问题是每个 class 一次Spring Boot运行器启动和停止其servlet容器。如果您使用WireMock规则,则每个 test方法都会启动和停止一次规则。这意味着在测试用例之间,Spring应用程序中的池化连接将变为无效。
您可以通过以下三种方法解决此问题: