use of dagger.internal.TestingLoader in project dagger by square.
the class ExtensionWithSetBindingsTest method basicInjectionWithExtension.
@Test
public void basicInjectionWithExtension() {
ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule());
RealSingleton rs = root.get(RealSingleton.class);
assertThat(rs.ints).containsExactly(0, 1);
ObjectGraph extension = root.plus(new ExtensionModule());
Main main = extension.get(Main.class);
assertThat(main.ints).containsExactly(0, 1, 2, 3);
// Second time around.
ObjectGraph extension2 = root.plus(new ExtensionModule());
Main main2 = extension2.get(Main.class);
assertThat(main2.ints).containsExactly(0, 1, 4, 5);
}
use of dagger.internal.TestingLoader in project dagger by square.
the class InjectStaticsTest method instanceFieldsNotInjectedByInjectStatics.
@Test
public void instanceFieldsNotInjectedByInjectStatics() {
@Module(staticInjections = InjectsStaticAndNonStatic.class, injects = InjectsStaticAndNonStatic.class)
class TestModule {
@Provides
String provideString() {
return "static";
}
@Provides
Integer provideInteger() {
throw new AssertionError();
}
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
assertThat(InjectsStaticAndNonStatic.staticField).isNull();
graph.injectStatics();
assertThat(InjectsStaticAndNonStatic.staticField).isEqualTo("static");
}
use of dagger.internal.TestingLoader in project dagger by square.
the class InjectStaticsTest method staticFieldsNotInjectedByInjectMembers.
@Test
public void staticFieldsNotInjectedByInjectMembers() {
@Module(staticInjections = InjectsStaticAndNonStatic.class, injects = InjectsStaticAndNonStatic.class)
class TestModule {
@Provides
String provideString() {
throw new AssertionError();
}
@Provides
Integer provideInteger() {
return 5;
}
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
assertThat(InjectsStaticAndNonStatic.staticField).isNull();
InjectsStaticAndNonStatic object = new InjectsStaticAndNonStatic();
graph.inject(object);
assertThat(InjectsStaticAndNonStatic.staticField).isNull();
assertThat(object.nonStaticField).isEqualTo(5);
}
use of dagger.internal.TestingLoader in project dagger by square.
the class InjectStaticsTest method injectStatics.
@Test
public void injectStatics() {
@Module(staticInjections = InjectsOneField.class)
class TestModule {
@Provides
String provideString() {
return "static";
}
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
assertThat(InjectsOneField.staticField).isNull();
graph.injectStatics();
assertThat(InjectsOneField.staticField).isEqualTo("static");
}
use of dagger.internal.TestingLoader in project dagger by square.
the class ExtensionTest method scopedGraphs.
@Test
public void scopedGraphs() {
ObjectGraph app = ObjectGraph.createWith(new TestingLoader(), new RootModule());
assertThat(app.get(A.class)).isNotNull();
assertThat(app.get(A.class)).isSameAs(app.get(A.class));
assertThat(app.get(B.class)).isNotSameAs(app.get(B.class));
assertFailInjectNotRegistered(app, C.class);
assertFailInjectNotRegistered(app, D.class);
ObjectGraph request1 = app.plus(new ExtensionModule());
ObjectGraph request2 = app.plus(new ExtensionModule());
for (ObjectGraph request : Arrays.asList(request1, request2)) {
assertThat(request.get(A.class)).isNotNull();
assertThat(request.get(A.class)).isSameAs(request.get(A.class));
assertThat(request.get(B.class)).isNotSameAs(request.get(B.class));
assertThat(request.get(C.class)).isNotNull();
assertThat(request.get(C.class)).isSameAs(request.get(C.class));
assertThat(request.get(D.class)).isNotSameAs(request.get(D.class));
}
// Singletons are one-per-graph-instance where they are declared.
assertThat(request1.get(C.class)).isNotSameAs(request2.get(C.class));
// Singletons that come from common roots should be one-per-common-graph-instance.
assertThat(request1.get(C.class).a).isSameAs(request2.get(C.class).a);
}
Aggregations