Vert.x单元测试不执行

时间:2017-03-09 01:02:39

标签: java unit-testing vert.x

我是vert.x的新手,并决定开始使用光,但无法进行简单的单元测试(使用maven构建)。代码如下

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

在等待TimeoutException时浏览http://localhost:8080/upload会显示一个Hello World!页面,但@Test似乎永远不会执行。我在这里做错了什么?

此致 的Mattias

1 个答案:

答案 0 :(得分: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());
                    }
                });
    }
}