注意:我正在使用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 *,測試運行器不會等待其他線程完成,你必須在你的測試方法中安排它。 –