Search in sources :

Example 46 with TestingLoader

use of dagger.internal.TestingLoader in project dagger by square.

the class InjectionTest method privateFieldsFail.

@Test
public void privateFieldsFail() {
    class Test {

        @Inject
        private Object nope;
    }
    @Module(injects = Test.class)
    class TestModule {

        @Provides
        Object provideObject() {
            return null;
        }
    }
    try {
        ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(new Test());
        fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).contains("Can't inject private field: ");
    }
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test) Test(org.junit.Test)

Example 47 with TestingLoader

use of dagger.internal.TestingLoader in project dagger by square.

the class InjectionTest method getInstance.

@Test
public void getInstance() {
    final AtomicInteger next = new AtomicInteger(0);
    @Module(injects = Integer.class)
    class TestModule {

        @Provides
        Integer provideInteger() {
            return next.getAndIncrement();
        }
    }
    ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
    assertThat((int) graph.get(Integer.class)).isEqualTo(0);
    assertThat((int) graph.get(Integer.class)).isEqualTo(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 48 with TestingLoader

use of dagger.internal.TestingLoader in project dagger by square.

the class InjectionTest method testSingletonLinkingThroughExtensionGraph.

@Test
public void testSingletonLinkingThroughExtensionGraph() {
    ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule());
    // DO NOT CALL root.get(C.class)) HERE to get forced-linking behaviour from plus();
    ObjectGraph extension = root.plus(new ExtensionModule());
    assertThat(extension.get(SingletonLinkedFromExtension.class).c).isSameAs(root.get(C.class));
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 49 with TestingLoader

use of dagger.internal.TestingLoader in project dagger by square.

the class InjectionTest method noConstructorInjectionsForClassesWithTypeParameters.

@Test
public void noConstructorInjectionsForClassesWithTypeParameters() {
    class TestEntryPoint {

        @Inject
        Parameterized<Long> parameterized;
    }
    @Module(injects = TestEntryPoint.class)
    class TestModule {

        @Provides
        String provideString() {
            return "injected";
        }
    }
    ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
    try {
        graph.validate();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : TestingLoader(dagger.internal.TestingLoader) Test(org.junit.Test)

Example 50 with TestingLoader

use of dagger.internal.TestingLoader in project dagger by square.

the class InjectionTest method noProvideBindingsForAbstractClasses.

@Test
public void noProvideBindingsForAbstractClasses() {
    class TestEntryPoint {

        @Inject
        AbstractList abstractList;
    }
    @Module(injects = TestEntryPoint.class)
    class TestModule {
    }
    ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
    try {
        graph.validate();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : AbstractList(java.util.AbstractList) 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