use of javax.inject.Provider in project dagger by square.
the class InjectionOfLazyTest method sideBySideLazyVsProvider.
@Test
public void sideBySideLazyVsProvider() {
final AtomicInteger counter = new AtomicInteger();
class TestEntryPoint {
@Inject
Provider<Integer> providerOfInteger;
@Inject
Lazy<Integer> lazyInteger;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides
Integer provideInteger() {
return counter.incrementAndGet();
}
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
assertEquals(0, counter.get());
assertEquals(0, counter.get());
assertEquals(1, ep.lazyInteger.get().intValue());
assertEquals(1, counter.get());
// fresh instance
assertEquals(2, ep.providerOfInteger.get().intValue());
// still the same instance
assertEquals(1, ep.lazyInteger.get().intValue());
assertEquals(2, counter.get());
// fresh instance
assertEquals(3, ep.providerOfInteger.get().intValue());
// still the same instance.
assertEquals(1, ep.lazyInteger.get().intValue());
}
use of javax.inject.Provider in project dagger by square.
the class InjectionOfLazyTest method providerOfLazyOfSomething.
@Test
public void providerOfLazyOfSomething() {
final AtomicInteger counter = new AtomicInteger();
class TestEntryPoint {
@Inject
Provider<Lazy<Integer>> providerOfLazyInteger;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
@Provides
Integer provideInteger() {
return counter.incrementAndGet();
}
}
TestEntryPoint ep = injectWithModule(new TestEntryPoint(), new TestModule());
assertEquals(0, counter.get());
Lazy<Integer> i = ep.providerOfLazyInteger.get();
assertEquals(1, i.get().intValue());
assertEquals(1, counter.get());
assertEquals(1, i.get().intValue());
Lazy<Integer> j = ep.providerOfLazyInteger.get();
assertEquals(2, j.get().intValue());
assertEquals(2, counter.get());
assertEquals(1, i.get().intValue());
}
use of javax.inject.Provider in project dagger by square.
the class MembersInjectorTest method instanceInjectionOfMembersOnlyType.
@Test
public void instanceInjectionOfMembersOnlyType() {
class TestEntryPoint {
@Inject
Provider<Unconstructable> provider;
}
@Module(injects = TestEntryPoint.class)
class TestModule {
}
ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule());
try {
graph.get(TestEntryPoint.class);
fail();
} catch (IllegalStateException expected) {
}
}
Aggregations