2017-10-07 4 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) 

그 verticle 테스트 completition 후 종료를하지 않는 것 같다. 여기에 테스트가 있습니다 :

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