Exemplo n.º 1
0
 @Test
 public void autoInstantiationOfIncludedModules() {
   // Have to make these non-method-scoped or instantiation errors occur.
   ObjectGraph objectGraph = ObjectGraph.create(new TestModuleB()); // TestModuleA auto-created.
   assertThat(objectGraph.get(A.class)).isNotNull();
   assertThat(objectGraph.get(B.class).a).isNotNull();
 }
Exemplo n.º 2
0
  @Test
  public void childModuleWithChildModule() {

    @Module(injects = TestEntryPoint.class, includes = ModuleWithChildModule.class)
    class TestModule {}

    ObjectGraph objectGraph = ObjectGraph.create(new TestModule());
    TestEntryPoint entryPoint = new TestEntryPoint();
    objectGraph.inject(entryPoint);
    assertThat(entryPoint.s).isEqualTo("injected");
  }
Exemplo n.º 3
0
  @Test
  public void childModuleWithManualConstruction() {

    @Module(injects = TestEntryPoint.class, includes = ModuleWithConstructor.class)
    class TestModule {}

    ObjectGraph objectGraph = ObjectGraph.create(new ModuleWithConstructor("a"), new TestModule());
    TestEntryPoint entryPoint = new TestEntryPoint();
    objectGraph.inject(entryPoint);
    assertThat(entryPoint.s).isEqualTo("a");
  }
Exemplo n.º 4
0
  @Test
  public void childModuleWithEntryPoint() {
    @Module(includes = ModuleWithEntryPoint.class)
    class TestModule {
      @Provides
      String provideString() {
        return "injected";
      }
    }

    ObjectGraph objectGraph = ObjectGraph.create(new TestModule());
    TestEntryPoint entryPoint = objectGraph.get(TestEntryPoint.class);
    assertThat(entryPoint.s).isEqualTo("injected");
  }
Exemplo n.º 5
0
  @Test
  public void childModuleWithStaticInjection() {
    @Module(includes = ModuleWithStaticInjection.class)
    class TestModule {
      @Provides
      String provideString() {
        return "injected";
      }
    }

    ObjectGraph objectGraph = ObjectGraph.create(new TestModule());
    TestStaticInjection.s = null;
    objectGraph.injectStatics();
    assertThat(TestStaticInjection.s).isEqualTo("injected");
  }
Exemplo n.º 6
0
 @Test
 public void moduleExtendingClassThrowsException() {
   try {
     ObjectGraph.create(new ThreadModule());
     fail();
   } catch (IllegalArgumentException e) {
     assertThat(e.getMessage()).startsWith("Modules must not extend from other classes: ");
   }
 }
Exemplo n.º 7
0
  @Test
  public void childModuleMissingManualConstruction() {
    @Module(includes = ModuleWithConstructor.class)
    class TestModule {}

    try {
      ObjectGraph.create(new TestModule());
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }
Exemplo n.º 8
0
 @Test
 public void provideRawLazyFails() {
   @Module
   class ProvidesRawLazyModule {
     @Provides
     Lazy provideObject() {
       return null;
     }
   }
   try {
     ObjectGraph.create(new ProvidesRawLazyModule());
     fail();
   } catch (IllegalStateException e) {
     assertThat(e.getMessage()) //
         .startsWith("@Provides method must not return Lazy directly: ")
         .endsWith("ProvidesRawLazyModule.provideObject");
   }
 }
Exemplo n.º 9
0
 @Test(expected = IllegalArgumentException.class)
 public void childModuleMissingModuleAnnotation() {
   ObjectGraph.create(new ChildModuleMissingModuleAnnotation());
 }
Exemplo n.º 10
0
 @Test
 public void autoInstantiationOfModules() {
   // Have to make these non-method-scoped or instantiation errors occur.
   ObjectGraph objectGraph = ObjectGraph.create(TestModuleA.class);
   assertThat(objectGraph.get(A.class)).isNotNull();
 }
Exemplo n.º 11
0
 private <T> T injectWithModule(T ep, Object... modules) {
   // TODO(cgruber): Make og.inject(foo) return foo properly.
   ObjectGraph og = ObjectGraph.get(modules);
   og.inject(ep);
   return ep;
 }