2017-10-07 133 views
0

我在玩Vertx.io(版本3.4.2)。放下一些代碼,現在我想測試它,所以我寫了幾個單元測試。當我單獨運行各個測試他們都完成很好,但是當我做一個乾淨的MVN試驗均不能與此消息:單元測試Vertx.io應用程序

java.net.BindException: Address already in use: bind 
at sun.nio.ch.Net.bind0(Native Method) 
at sun.nio.ch.Net.bind(Unknown Source) 
at sun.nio.ch.Net.bind(Unknown Source) 
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) 
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128) 
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:554) 
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1258) 
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:502) 
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:487) 
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:980) 
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:250) 
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:365) 
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) 
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasksFrom(SingleThreadEventExecutor.java:379) 
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354) 
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:436) 
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) 
at java.lang.Thread.run(Unknown Source) 

看來測試completition之後verticle不關機。下面是測試:

@RunWith(VertxUnitRunner.class) 
public class SomeApiTest { 

private ObjectMapper objectMapper; 
private Vertx vertx; 
private Integer port; 
private static Logger log; 

@BeforeClass 
public static void initTests() { 
log = Logger.getLogger("SomeApiTest"); 
} 

@Before 
public void setUp(TestContext context) { 
log.info("start up test."); 
vertx = Vertx.vertx(); 
objectMapper = new ObjectMapper(); 
port = 8080; 
DeploymentOptions options = new DeploymentOptions(); 
options.setConfig(new JsonObject()); 
options.getConfig().put("api.port", port); 
vertx.deployVerticle("SomeApi", options, context.asyncAssertSuccess()); 
} 

@After 
public void tearDown(TestContext context) { 
log.info("shutting down test."); 
vertx.close(h -> { 
    if (h.failed()) { 
    log.error(h.cause()); 
    } 
}); 
} 

@Test 
public void whenRequestHitsVerticleItReturnsRecipes(TestContext context) throws Exception { 
final Async async = context.async(); 
List<Integer> votes = new ArrayList<>(); 
votes.add(new Integer(6)); 
votes.add(new Integer(4)); 
votes.add(new Integer(8)); 
vertx.eventBus().consumer("persistence", h -> { 
    h.reply(Json.encode(votes)); 
}); 
vertx.createHttpClient().getNow(port, "localhost", "/votes?qty=3", response -> { 
    response.handler(body -> { 
    Integer[] xvotes; 
    try { 
     xvotes = objectMapper.readValue(body.toString(), Integer[].class); 
     int expected = 3; 
     int actual = xvotes.length; 
     Assert.assertEquals(expected, actual); 
     async.complete(); 
    } catch (Exception e) { 
     log.error(e); 
     Assert.fail(e.getMessage()); 
    } 
    }); 
}); 
} 

@Test 
public void fiveDishesRequestAgainsTenRecipesApi(TestContext context) throws Exception { 
    final Async async = context.async(); 
    List<Integer> votes = new ArrayList<>(); 
    votes.add(new Integer(3)); 
    votes.add(new Integer(4)); 
    votes.add(new Integer(7)); 
    votes.add(new Integer(7)); 
    votes.add(new Integer(6)); 
    vertx.eventBus().consumer("persistence", h -> { 
    h.reply(Json.encode(votes)); 
    }); 
    vertx.createHttpClient().getNow(port, "localhost", "/votes?qty=5", response -> { 
    response.handler(body -> { 
    Integer[] xvotes; 
    try { 
     xvotes = objectMapper.readValue(body.toString(), Integer[].class); 
     int expected = 5; 
     int actual = xvotes.length; 
     Assert.assertEquals(expected, actual); 
     async.complete(); 
    } catch (Exception e) { 
     log.error(e); 
     Assert.fail(e.getMessage()); 
    } 
    }); 
    }); 
    } 
} 

測試很簡單,他們的目的是讓如何單元測試與Vertx。我期待在測試結束後每個Verticle將被部署。我錯過了什麼?

回答

1

嘗試等待至vertx已關閉。這裏是例子:

@After 
public void tearDown(TestContext context) { 
log.info("shutting down test."); 
Async async = context.async(); 
vertx.close(h -> { 
    if (h.failed()) { 
     log.error(h.cause()); 
    } 
    async.success(); 
}); 
}