2017-10-07 3 views
0

Je joue avec Vertx.io (version 3.4.2). Mettez du code et maintenant je veux le tester, alors j'écris quelques tests unitaires. Quand je lance chaque test séparément ils ont tous deux bien complet, mais quand je fais un test mvn propre ils ont tous deux échouent avec ce message:Unité testant une application 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) 

Il semble que verticle ne se arrête pas après completition test. Voici les tests:

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

Les tests sont simples, leur but est d'obtenir comment effectuer un test unitaire avec Vertx. Je m'attends à ce que chaque verticule ne soit pas déployée après la fin du test. Qu'est-ce que je rate?

Répondre

1

Essayez d'attendre jusqu'à ce que vertx soit fermé. Voici l'exemple:

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