Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jukito.example.override;
- import com.google.inject.AbstractModule;
- import com.google.inject.Module;
- import com.google.inject.util.Modules;
- import org.jukito.*;
- import org.junit.*;
- import org.junit.runner.*;
- import static org.junit.Assert.assertEquals;
- /**
- * A sample interface.
- */
- interface Foo {
- String name();
- }
- /**
- * The "normal" implementation of Foo.
- */
- class FooImpl implements Foo {
- public String name() {
- return "FooImpl";
- }
- }
- /**
- * The "normal" binding of Foo.
- */
- class FooModule extends AbstractModule {
- protected void configure() {
- bind(Foo.class).to(FooImpl.class);
- }
- }
- /**
- * Common behavior for several tests. Could have protected
- * methods used by several concrete test subclasses.
- */
- class TestBase {
- /**
- * Bindings that are sufficiently commonly used by
- * concrete subclasses of TestBase to warrant packaging
- * as a single module.
- */
- public static Module commonBindings() {
- return Modules.combine(
- new FooModule()
- // ... other common bindings would go here ...
- );
- }
- }
- /**
- * An implementation of Foo used for testing purposes.
- */
- class TestFooImpl implements Foo {
- public String name() {
- return "TestFooImpl";
- }
- }
- /**
- * The test binding of Foo.
- */
- class TestFooModule extends AbstractModule {
- protected void configure() {
- bind(Foo.class).to(TestFooImpl.class);
- }
- }
- /**
- * A concrete test class, extending TestBase.
- */
- @RunWith(JukitoRunner.class)
- public class ConcreteTest extends TestBase {
- /**
- * Overrides the common bindings from TestBase with the
- * module that has test-specific bindings for Foo.
- */
- public static class Module extends JukitoModule {
- protected void configureTest() {
- install(Modules.override(commonBindings()).with(new TestFooModule()));
- }
- }
- /**
- * It's pretty silly to test our own bindings, but this is
- * just to verify that Modules.override really works. (It does!)
- */
- @Test public void testFooShouldReturnTestFooImpl(Foo foo) {
- assertEquals("TestFooImpl", foo.name());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement