Eug*_*gun 4 java post unit-testing reactive-programming vert.x
我有具有最简单授权的vert.x应用服务器,其路由如下:
router.post("/user/sign").handler(this::userSignIn);
Run Code Online (Sandbox Code Playgroud)
当我从GUI执行此操作时,它工作正常。
但我想为此操作创建单元测试。我做的代码:
@Test
public void testServerUserRegister(TestContext context) {
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = client.post("user/sign", response -> {
System.out.println("Some callback " + response.statusCode());
});
String body = "{'username':'www','password':'www'}";
request.putHeader("content-length", "1000");
request.putHeader("content-type", "application/x-www-form-urlencoded");
request.write(body);
request.end();
}
Run Code Online (Sandbox Code Playgroud)
当我开始测试时: mvn test
我看到了结果,但:
1-我看不到任何字符串,例如“ Some callback ...”
2-我得到一个RejectedExecutionException:
???. 16, 2016 4:29:20 PM io.vertx.core.http.impl.HttpClientImpl
SEVERE: java.nio.channels.ClosedChannelException
???. 16, 2016 4:29:20 PM io.netty.channel.AbstractChannel$AbstractUnsafe invokeLater
WARNING: Can't invoke task later as EventLoop rejected it
java.util.concurrent.RejectedExecutionException: event executor terminated
at io.netty.util.concurrent.SingleThreadEventExecutor
.reject(SingleThreadEventExecutor.java:707)
at io.netty.util.concurrent.SingleThreadEventExecutor
.addTask(SingleThreadEventExecutor.java:299)
at io.netty.util.concurrent.SingleThreadEventExecutor
.execute(SingleThreadEventExecutor.java:687)
at io.netty.channel.AbstractChannel$AbstractUnsafe
.invokeLater(AbstractChannel.java:826)
at io.netty.channel.AbstractChannel$AbstractUnsafe
.deregister(AbstractChannel.java:656)
at io.netty.channel.AbstractChannel$AbstractUnsafe
.fireChannelInactiveAndDeregister(AbstractChannel.java:626)
at io.netty.channel.AbstractChannel$AbstractUnsafe
.close(AbstractChannel.java:600)
at io.netty.channel.nio.NioEventLoop.closeAll(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:361)
at io.netty.util.concurrent.SingleThreadEventExecutor$2
.run(SingleThreadEventExecutor.java:111)
at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)
知道我在做什么错吗?
调用之后request.end(),JUnit测试退出,并且您的Vert.x实例关闭(因此RejectedExecutionException)。
请记住,Vert.x API是异步的。看一下有关异步测试的Vert.x单元部分。
您应该执行的操作如下:
@Test
public void testServerUserRegister(TestContext context) {
// Get an async object to control the completion of the test
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = client.post("user/sign", response -> {
// You may want to check response code here
// to either complete or fail the test
async.complete();
System.out.println("Some callback " + response.statusCode());
});
String body = "{'username':'www','password':'www'}";
request.putHeader("content-length", "1000");
request.putHeader("content-type", "application/x-www-form-urlencoded");
request.write(body);
request.end();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5313 次 |
| 最近记录: |