- Add the following class to your classpath:
package com.google.inject.junit; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; import java.util.List; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.InitializationError; /** * Uses Guice to inject JUnit4 tests. * * @author Gili Tzabari */ public class GuiceTestRunner extends BlockJUnit4ClassRunner { private final Injector injector; /** * Creates a new GuiceTestRunner. * * @param classToRun the test class to run * @param modules the Guice modules * @throws InitializationError if the test class is malformed */ public GuiceTestRunner(final Class<?> classToRun, Module... modules) throws InitializationError { super(classToRun); this.injector = Guice.createInjector(modules); } @Override public Object createTest() { return injector.getInstance(getTestClass().getJavaClass()); } @Override protected void validateZeroArgConstructor(List<Throwable> errors) { // Guice can inject constructors with parameters so we don't want this method to trigger an error } /** * Returns the Guice injector. * * @return the Guice injector */ protected Injector getInjector() { return injector; } }
- Customize GuiceTestRunner for your specific project by subclassing it. For example:
package myproject; import com.google.inject.junit.GuiceTestRunner; import com.wideplay.warp.persist.PersistenceService; import myproject.GuiceModule; import org.junit.runners.model.InitializationError; /** * JUnit4 runner customized for our Guice module. * * @author Gili Tzabari */ public class GuiceIntegration extends GuiceTestRunner { /** * Creates a new GuiceIntegration. * * @param classToRun the test class to run * @throws InitializationError if the test class is malformed */ public GuiceIntegration(Class classToRun) throws InitializationError { super(classToRun, new GuiceModule()); } }
- Add @RunWith to all your JUnit test classes. For example:
@RunWith(GuiceIntegration.class) public class MyTest { @Inject public MyTest(SomeDependency foo) { ... } }
That's it! Guice will now inject your test classes.
EDIT: I'm a convert :) I now use AtUnit to integrate Guice into JUnit.