Advertisement
tpeierls

Overriding bindings in Jukito

Dec 6th, 2011
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.22 KB | None | 0 0
  1. package org.jukito.example.override;
  2.  
  3. import com.google.inject.AbstractModule;
  4. import com.google.inject.Module;
  5. import com.google.inject.util.Modules;
  6.  
  7. import org.jukito.*;
  8. import org.junit.*;
  9. import org.junit.runner.*;
  10. import static org.junit.Assert.assertEquals;
  11.  
  12. /**
  13.  * A sample interface.
  14.  */
  15. interface Foo {
  16.     String name();
  17. }
  18.  
  19. /**
  20.  * The "normal" implementation of Foo.
  21.  */
  22. class FooImpl implements Foo {
  23.     public String name() {
  24.         return "FooImpl";
  25.     }
  26. }
  27.  
  28. /**
  29.  * The "normal" binding of Foo.
  30.  */
  31. class FooModule extends AbstractModule {
  32.     protected void configure() {
  33.         bind(Foo.class).to(FooImpl.class);
  34.     }
  35. }
  36.  
  37. /**
  38.  * Common behavior for several tests. Could have protected
  39.  * methods used by several concrete test subclasses.
  40.  */
  41. class TestBase {
  42.     /**
  43.      * Bindings that are sufficiently commonly used by
  44.      * concrete subclasses of TestBase to warrant packaging
  45.      * as a single module.
  46.      */
  47.     public static Module commonBindings() {
  48.         return Modules.combine(
  49.             new FooModule()
  50.             // ... other common bindings would go here ...
  51.         );
  52.     }
  53. }
  54.  
  55. /**
  56.  * An implementation of Foo used for testing purposes.
  57.  */
  58. class TestFooImpl implements Foo {
  59.     public String name() {
  60.         return "TestFooImpl";
  61.     }
  62. }
  63.  
  64. /**
  65.  * The test binding of Foo.
  66.  */
  67. class TestFooModule extends AbstractModule {
  68.     protected void configure() {
  69.         bind(Foo.class).to(TestFooImpl.class);
  70.     }
  71. }
  72.  
  73. /**
  74.  * A concrete test class, extending TestBase.
  75.  */
  76. @RunWith(JukitoRunner.class)
  77. public class ConcreteTest extends TestBase {
  78.  
  79.     /**
  80.      * Overrides the common bindings from TestBase with the
  81.      * module that has test-specific bindings for Foo.
  82.      */
  83.     public static class Module extends JukitoModule {
  84.         protected void configureTest() {
  85.             install(Modules.override(commonBindings()).with(new TestFooModule()));
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * It's pretty silly to test our own bindings, but this is
  91.      * just to verify that Modules.override really works. (It does!)
  92.      */
  93.     @Test public void testFooShouldReturnTestFooImpl(Foo foo) {
  94.         assertEquals("TestFooImpl", foo.name());
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement