Exemple #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();
 }
Exemple #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");
  }
Exemple #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");
  }
Exemple #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");
  }
Exemple #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");
  }
Exemple #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: ");
   }
 }
Exemple #7
0
  @Test
  public void childModuleMissingManualConstruction() {
    @Module(includes = ModuleWithConstructor.class)
    class TestModule {}

    try {
      ObjectGraph.create(new TestModule());
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }
Exemple #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");
   }
 }
Exemple #9
0
 @Test(expected = IllegalArgumentException.class)
 public void childModuleMissingModuleAnnotation() {
   ObjectGraph.create(new ChildModuleMissingModuleAnnotation());
 }
Exemple #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();
 }
Exemple #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;
 }