我有一个创建HttpClientRequest实例并将其关联到Handler的方法。
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); }
您可以假设所有变量都根据需要进行了模拟。
我希望测试在request.handler
方法中调用sendRequest
。验证整个流程最多执行request.end
。
答案 0 :(得分:1)
我建议使用Wiremock模拟该第三方服务的响应:
@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();
});
}
}
答案 1 :(得分:0)
我能够使用ArgumentCaptor完成它。
以下是有助于实现上述目标的代码(上述测试的一部分)。
声明ArgumentCaptor:
@Captor private ArgumentCaptor<Handler<HttpClientResponse>> requestCaptor;
在测试中使用捕获器:
vertx.eventBus().consumer("test-bus", (Message<A> message) -> { syncClient.sendRequest(requestId, file, message); Mockito.verify(httpClientRequest, Mockito.times(1)).handler(requestCaptor.capture()); Handler<HttpClientResponse> httpClientResponseHandler = requestCaptor.getValue(); httpClientResponseHandler.handle(httpClientResponse); });
说明:ArgumentCaptor捕获执行流,然后可以使用所需的参数手动触发进一步的执行。它仅适用于Mockito.verify
。