gpt4 book ai didi

java - Vertx HttpClientRequest 处理程序行为测试

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:19 27 4
gpt4 key购买 nike

我有一个创建 HttpClientRequest 实例并将处理程序与其关联的方法。

public void sendRequest(String requestId, File file, Message<A> message) {

final HttpClientRequest request = getHttpClientRequest();
request.putHeader(HttpHeaders.CONTENT_TYPE.toString(), FORM_DATA);
request.putHeader(HttpHeaders.ACCEPT.toString(), APPNEXUS_JSON_HEADER);
request.putHeader(HttpHeaders.CONTENT_TRANSFER_ENCODING.toString(), "binary");
final Buffer buffer = this.getBody(file.getAbsolutePath());
request.putHeader(HttpHeaders.CONTENT_LENGTH.toString(), String.valueOf(buffer.length()));
request.handler(httpClientResponse -> {
switch (httpClientResponse.statusCode()) {
case Status.SC_OK:
httpClientResponse.bodyHandler(body -> {
// Do something
});
break;
case Status.TOO_MANY_REQUESTS:
// Do something
break;
default:
// Do something
}
});}

客户端请求是向第三方服务发出的。我应该如何编写单元测试来调用处理程序的不同子句?我正在使用 Mockito 来执行模拟任务。

到目前为止我已经编写的测试,

public void testSomething (TestContext testContext) { 
final Async async = testContext.async();
Mockito.when(httpClientRequest.exceptionHandler(Mockito.any())).thenReturn(httpClientRequest);
Mockito.when(httpClientRequest.putHeader(Mockito.anyString(), Mockito.anyString())).thenReturn(httpClientRequest);
Mockito.doAnswer(invocation -> {
return httpClientResponse;
}).when(httpClientRequest).end(Mockito.any(Buffer.class));
Mockito.when(routingContext.response()).thenReturn(httpServerResponse);
Mockito.when(routingContext.statusCode()).thenReturn(200);
Mockito.when(routingContext.getBody()).thenReturn(buffer);
JsonObject jsonObject = Mockito.mock(JsonObject.class);
Mockito.when(buffer.toJsonObject()).thenReturn(jsonObject); Mockito.when(jsonObject.mapTo(Mockito.any())).thenReturn(appnexusBulkSyncResponse);
Mockito.when(file.getAbsolutePath()).thenReturn("testpath");
String requestId = "req-1";
JsonObject uploadRequest = new JsonObject();
uploadRequest.put("requestId", requestId);
vertx.eventBus().consumer("test-bus", (Message<A> message) -> {
syncClient.sendRequest(requestId, file, message);
});
vertx.eventBus().send("test-bus", uploadRequest, event -> {
async.complete();
});
async.await(TIMEOUT);
}

您可以假设所有变量都根据需要进行了模拟。我希望测试调用 sendRequest 方法中的 request.handler 。已验证整个流程执行到 request.end

最佳答案

我建议使用 Wiremock 模拟第 3 方服务的响应:

@RunWith(VertxUnitRunner.class)
public class MyTestClass {

private static WireMockServer wiremock;
private Vertx vertx;

@BeforeClass
public static void init() {
wiremock = new WireMockServer(host, port); //configure host and port
wiremock.start();
}

@AfterClass
public static void cleanup() {
wiremock.stop();
}

@Before
public void setup(TestContext ctx){
//init vertx
Async async = testContext.async();
vertx = Vertx.vertx();
...
async.complete();
}

@Test
public void mytest(TestContext testContext){
Async async = testContext.async();
stubFor(get(urlPathMatching("/.*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{}")));

HttpClient client = vertx.createHttpClient(...)
client.getNow("/some-uri", response -> {
//check response
async.complete();
});
}

}

关于java - Vertx HttpClientRequest 处理程序行为测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56939961/

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