Skip to content

Instantly share code, notes, and snippets.

@nlativy
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlativy/a39273ef3ad268ab77eb to your computer and use it in GitHub Desktop.
Save nlativy/a39273ef3ad268ab77eb to your computer and use it in GitHub Desktop.
GuiceRule - rule for using Guice in JUnit4 tests
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.inject.Guice;
import com.google.inject.Module;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class GuiceRule implements MethodRule {
private final Module module;
public GuiceRule(Module module) {
this.module = checkNotNull(module);
}
@Override
public Statement apply(final Statement statement,
FrameworkMethod frameworkMethod, final Object target) {
return new Statement() {
@Override public void evaluate() throws Throwable {
Guice.createInjector(module).injectMembers(target);
statement.evaluate();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment