Search in sources :

Example 56 with TestingLoader

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);
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 57 with TestingLoader

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");
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 58 with TestingLoader

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);
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 59 with TestingLoader

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");
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 60 with TestingLoader

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);
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Aggregations

TestingLoader (dagger.internal.TestingLoader)65 Test (org.junit.Test)65 ArrayList (java.util.ArrayList)3 Collections.emptySet (java.util.Collections.emptySet)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AbstractList (java.util.AbstractList)2 Callable (java.util.concurrent.Callable)2 Future (java.util.concurrent.Future)2 List (java.util.List)1 RandomAccess (java.util.RandomAccess)1 Provider (javax.inject.Provider)1