2017-03-09 87 views
0

我是vert.x新手,決定開始使用light,但無法獲得簡單的單元測試(使用maven構建)。代碼如下Vert.x單元測試不執行

FileRepo.java:

import io.vertx.core.AbstractVerticle; 
import io.vertx.core.Future; 
import io.vertx.core.http.HttpServer; 
import io.vertx.core.http.HttpServerResponse; 
import io.vertx.ext.web.Router; 

public class FileRepo extends AbstractVerticle { 
    @Override 
    public void start(Future<Void> fut) { 
    HttpServer server = vertx.createHttpServer(); 
    Router router = Router.router(vertx); 
    router.route("/upload").handler(routingContext -> { 
     HttpServerResponse response = routingContext.response(); 
     response.putHeader("content-type", "text/plain"); 
     response.end("Hello world!"); 
    }); 
    System.out.println("Starting server!"); 
    server.requestHandler(router::accept).listen(8080); 
    System.out.println("Server started!"); 
    } 
} 

FileRepoTest.java:

import FileRepo; 
import io.vertx.core.Vertx; 
import io.vertx.ext.unit.TestContext; 
import io.vertx.ext.unit.junit.VertxUnitRunner; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

@RunWith(VertxUnitRunner.class) 
public class FileRepoTest { 
    private Vertx vertx; 

    @Before 
    public void setUp(TestContext context) { 
    vertx = Vertx.vertx(); 
    System.out.println("SetUp Vertx"); 
    vertx.deployVerticle(FileRepo.class.getName(), context.asyncAssertSuccess()); 
    System.out.println("SetUp done"); 
    } 

    @After 
    public void tearDown(TestContext context) { 
    System.out.println("tearDown Vertx"); 
    vertx.close(context.asyncAssertSuccess()); 
    } 

    @Test 
    public void testUpload(TestContext context) { 
    System.out.println("testUpload"); 
    } 
} 

結果:

SetUp Vertx 
SetUp done 
Starting server! 
Server started! 
tearDown Vertx 

java.util.concurrent.TimeoutException 
    at io.vertx.ext.unit.impl.TestContextImpl$Step.lambda$run$0(TestContextImpl.java:112) 
    at java.lang.Thread.run(Thread.java:745) 

Process finished with exit code -1 

瀏覽http://localhost:8080/upload等待TimeoutException顯示Hello World!頁面,但@Test似乎從未執行。我在這裏做錯了什麼?

問候, 馬蒂亞斯

回答

2

你所得到的例外是因爲沒有服務器啓動狀態的確認。

您Verticle更改爲以下幾點:

import io.vertx.core.AbstractVerticle; 
import io.vertx.core.Future; 
import io.vertx.core.http.HttpServerResponse; 
import io.vertx.ext.web.Router; 

public class FileRepo extends AbstractVerticle { 
    @Override 
    public void start(Future<Void> fut) { 
     Router router = Router.router(vertx); 
     router.route("/upload").handler(routingContext -> { 
      HttpServerResponse response = routingContext.response(); 
      response.putHeader("content-type", "text/plain"); 
      response.end("Hello world!"); 
     }); 
     System.out.println("Starting server!"); 
     vertx.createHttpServer() 
       .requestHandler(router::accept) 
       .listen(8080, result -> { 
        if (result.succeeded()) { 
         System.out.println("Server started!"); 
         fut.complete(); 
        } else { 
         System.out.println("Server start failed!"); 
         fut.fail(result.cause()); 
        } 
       }); 
    } 
} 
+0

那麼明顯,咫尺天涯:)非常感謝。 – MattiasH