- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个创建 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/
我正在尝试创建一些简单的单元测试,这是代码: import 'dart:io'; import 'package:unittest/unittest.dart'; void main() { te
我无法捕获 vertx HttpClientRequest 中发生的超时异常。我已将连接和请求创建代码包含在 try-catch block 中。我还添加了异常处理程序和结束处理程序。但当超时发生时,
我有一个创建 HttpClientRequest 实例并将处理程序与其关联的方法。 public void sendRequest(String requestId, File file, Messa
有没有办法设置mimeType的 HttpClientRequest ? 我正在尝试做一些单元测试,我创建了一个 HttpClient 的实例。然后将一个带有字节数据的 post 请求发送到 Http
将 Flutter 更新为 后大师 channel ,之前编译的代码片段开始无法构建,并出现以下错误: ../pub.dartlang.org/testfairy-1.x.y/lib/src/netw
我是一名优秀的程序员,十分优秀!