Nota: Sto utilizzando JUnit 4.11 e Netty 3.6.5.JUnit e Netty causano la fine prematura dell'applicazione
Sto provando a testare alcune funzionalità di base nella mia complicata app del server. Voglio estrarre semplicemente la funzionalità di rete e fare alcuni test unitari. Tuttavia, quando provo a creare un unit test, l'applicazione esce semplicemente. Tuttavia, se metto un dummy public static void main
, funziona correttamente, ma al di fuori di JUnit, ovviamente. Ecco il 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();
}
}
Puoi eseguire il debug in NioServer? – fge
@fge Sì, il metodo 'run' ha un debug' println' che succede – durron597
Le cose piccole sono * asincrone *, il test runner non aspetta che finiscano altri thread, devi organizzarlo nel tuo metodo di test. –