2016-09-16 58 views
2

我有vert.x應用服務器與簡單的授權,其中有一些像這樣的路線:vert.x:你如何正確發送帖子請求?

router.post("/user/sign").handler(this::userSignIn); 

雖然我是從GUI這樣做,它工作正常。我想爲此操作創建Unit-Test。 代碼我所做的:

@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(); 
} 

當我開始這個測試:mvn test

我看到的結果,但:

1 - 我沒有看到像任何字符串「一些回調。」

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) 

不知道我在這裏做錯了什麼?

回答

4

在呼叫request.end()之後,JUnit測試退出並且您的Vert.x實例關閉(因此爲RejectedExecutionException)。

請記住,Vert.x API是異步的。看看Vert.x Unit section about asynchronous testing

你應該做的是一樣的東西:

@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(); 
}