注意:我使用的是 JUnit 4.11 和 Netty 3.6.5。
我正在尝试在我复杂的服务器应用程序中测试一些基本功能。我想简单地提取网络功能并进行一些单元测试。但是,当我尝试创建单元测试时,应用程序只是退出了。但是,如果我放一个 dummy 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();
}
}