我正在使用Vertx.io(版本3.4.2)。放下一些代码,现在我想测试它,所以我写了几个单元测试。当我单独运行每个测试时,它们都完好无损,但是当我进行mvn clean测试时,它们都会失败并显示以下消息:
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)
在测试完成后,似乎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。我错过了什么?
答案 0 :(得分: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();
});
}