Search in sources :

Example 81 with Injector

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());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 82 with Injector

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 83 with Injector

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());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 84 with Injector

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());
}
Also used : Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) Callable(java.util.concurrent.Callable) AbstractModule(com.google.inject.AbstractModule)

Example 85 with Injector

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);
}
Also used : Injector(com.google.inject.Injector) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) AbstractModule(com.google.inject.AbstractModule)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55