I have vert.x app-server with simplest authorization, which has some route like this:
router.post("/user/sign").handler(this::userSignIn);
While I'm doing this from the GUI, it works fine.
But I want to create Unit-Test for this action. The code I did:
@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();
}
When I start this test: mvn test
I see results but:
1- I don't see any String like "Some callback..."
2- I'm getting a 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)
Any idea what am I doing wrong here?
Vert. x is a toolkit used for building reactive applications on the JVM using an asynchronous and non-blocking execution model. As it is based on Netty, which is an event-driven and asynchronous network application framework, Vert.
Interface RoutingContext. Represents the context for the handling of a request in Vert. x-Web. A new instance is created for each HTTP request that is received in the Handler.
Vert. x-Web is a set of building blocks for building web applications with Vert. x.
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. @FunctionalInterface public interface Handler<E> A generic event handler. This interface is used heavily throughout Vert. x as a handler for all types of asynchronous occurrences.
After the call to request.end()
, the JUnit test exits and your Vert.x instance is closed (hence the RejectedExecutionException).
Remember that Vert.x APIs are asynchronous. Have a look at the Vert.x Unit section about asynchronous testing.
What you should do is something like:
@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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With