注意:我正在使用JUnit 4.11和Netty 3.6.5。JUnit和Netty导致应用程序提前终止
我想在我复杂的服务器应用程序中测试一些基本功能。我想简单地提取网络功能并做一些单元测试。但是,当我尝试创建单元测试时,应用程序会退出。但是,如果我放了一个虚拟public static void main
,它显然可以正常工作,但在JUnit之外。这里的sscce:
public class SimpleNetwork {
private Injector inj;
@Before
public void startInjector() {
Module mod = new AbstractModule() {
@Override
protected void configure() {
// Guice stuff you don't need to see, it works fine
}
};
inj = Guice.createInjector(mod);
}
// **When I run this using JUnit, the application ends immediately.**
@Test
public void testNetwork() {
NioServer server = inj.getInstance(NioServer.class);
server.run();
// **This prints in both scenarios**
System.out.println("Hello World");
}
// **When I run this, the application works as expected.**
public static void main(String[] args) {
SimpleNetwork sn = new SimpleNetwork();
sn.startInjector();
sn.testNetwork();
}
}
你可以调试到NioServer? – fge
@fge是的,'run'方法有一个调试'println'发生 – durron597
Netty stuff是* asynchronous *,测试运行器不会等待其他线程完成,你必须在你的测试方法中安排它。 –