JUnice core - The JUniceRunner
The core of JUnice is the org.nnsoft.guice.junice.JUniceRunner class, that's a JUnit Runner and extends org.junit.internal.runners.BlockJUnit4ClassRunner For each test case the JUniceRunner initializes a google-guice injector before that your test case class is instantiated.
So you have an injector ready to use already into any method @BeforeClass.
Getting Started
To use JUnice users have to annotate the junit test class in the following way:
@RunWith(JUniceRunner.class) public class SimpleTest { @com.google.inject.Inject private com.google.inject.Injector injector; @org.junit.BeforeClass public static void setUpYourClass(){ // use injector. injector.getInstance(AcmeService.class); } }
The JUnice runner can be used also in a super class:
@com.google.inject.ImplementedBy(AcmeDatabasePoolImpl.class) public interface AcmeDatabasePool { void open(); void close(); }
@RunWith(JUniceRunner.class) abstract public class DatabasePoolInitTest { @com.google.inject.Inject private static AcmeDatabasePool pool; @org.junit.BeforeClass public static void setUpYourClass(){ pool = injector.getInstance(AcmeDatabasePool.class); pool.open(); ... } @org.junit.AfterClass public static void setUpYourClass(){ ... pool.close(); ... } }
public class DatabaseClassTest extends DatabasePoolInitTest{ @org.junit.Test public void test(){ ... } }