use of com.google.inject.Injector in project guice by google.
the class DefaultMethodInterceptionTest method testInterception_ofAllMethodsOnType_interceptsInheritedDefaultMethod.
public void testInterception_ofAllMethodsOnType_interceptsInheritedDefaultMethod() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.subclassesOf(BazImpl.class), Matchers.any(), interceptor);
bind(Baz.class).to(BazImpl.class);
}
});
Baz baz = injector.getInstance(Baz.class);
assertEquals("Baz", baz.doSomething());
assertEquals("BazImpl", baz.doSomethingElse());
assertEquals(2, interceptedCallCount.get());
}
use of com.google.inject.Injector in project guice by google.
the class Java8LanguageFeatureBindingTest method testBinding_toProvider_lambda.
public void testBinding_toProvider_lambda() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
AtomicInteger i = new AtomicInteger();
bind(String.class).toProvider(() -> "Hello" + i.incrementAndGet());
}
});
assertEquals("Hello1", injector.getInstance(String.class));
assertEquals("Hello2", injector.getInstance(String.class));
}
use of com.google.inject.Injector in project guice by google.
the class Java8LanguageFeatureBindingTest method testBinding_toProvider_methodReference.
public void testBinding_toProvider_methodReference() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toProvider(Java8LanguageFeatureBindingTest.this::provideString);
}
});
Provider<String> provider = injector.getProvider(String.class);
assertEquals("Hello", provider.get());
}
use of com.google.inject.Injector in project guice by google.
the class Java8LanguageFeatureBindingTest method testProviderMethod_returningLambda.
public void testProviderMethod_returningLambda() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
}
@Provides
public Callable<String> provideCallable() {
return () -> "foo";
}
});
Callable<String> callable = injector.getInstance(new Key<Callable<String>>() {
});
assertEquals("foo", callable.call());
}
use of com.google.inject.Injector in project guice by google.
the class StaticInterfaceMethodsTest method testAssistedInjection.
public void testAssistedInjection() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(Factory.class));
}
});
Factory factory = injector.getInstance(Factory.class);
assertEquals(1, factory.create(1).i);
}
Aggregations