Last active
August 29, 2015 14:10
-
-
Save nlativy/a39273ef3ad268ab77eb to your computer and use it in GitHub Desktop.
GuiceRule - rule for using Guice in JUnit4 tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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