Search in sources :

Example 11 with ConfigurationException

use of com.google.inject.ConfigurationException in project guice by google.

the class MapBinderTest method testMapBinderMultimapWithAnotation.

public void testMapBinderMultimapWithAnotation() {
    AbstractModule ab1 = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            multibinder.addBinding("a").toInstance("A");
            multibinder.addBinding("b").toInstance("B1");
        }
    };
    AbstractModule b2c = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            multibinder.addBinding("b").toInstance("B2");
            multibinder.addBinding("c").toInstance("C");
            multibinder.permitDuplicates();
        }
    };
    Injector injector = Guice.createInjector(ab1, b2c);
    assertEquals(mapOf("a", setOf("A"), "b", setOf("B1", "B2"), "c", setOf("C")), injector.getInstance(Key.get(mapOfSetOfString, Abc.class)));
    try {
        injector.getInstance(Key.get(mapOfSetOfString));
        fail();
    } catch (ConfigurationException expected) {
    }
    assertMapVisitor(Key.get(mapOfString, Abc.class), stringType, stringType, setOf(ab1, b2c), BOTH, true, 0, instance("a", "A"), instance("b", "B1"), instance("b", "B2"), instance("c", "C"));
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 12 with ConfigurationException

use of com.google.inject.ConfigurationException in project guice by google.

the class ChildBindingAlreadySetErrorTest method childBindingAlreadySetError.

@Test
public void childBindingAlreadySetError() throws Exception {
    Injector injector = Guice.createInjector(new ChildModule());
    ConfigurationException exception = assertThrows(ConfigurationException.class, () -> injector.getInstance(Foo.class));
    assertGuiceErrorEqualsIgnoreLineNumber(exception.getMessage(), "child_binding_already_set_error.txt");
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) Test(org.junit.Test)

Example 13 with ConfigurationException

use of com.google.inject.ConfigurationException in project guice by google.

the class ChildBindingAlreadySetErrorTest method childBindingAlreadySetMultipleTimesError.

@Test
public void childBindingAlreadySetMultipleTimesError() throws Exception {
    Injector injector = Guice.createInjector(new ChildModule(), new ChildModule2());
    ConfigurationException exception = assertThrows(ConfigurationException.class, () -> injector.getInstance(DependsOnFoo.class));
    assertGuiceErrorEqualsIgnoreLineNumber(exception.getMessage(), "child_binding_already_set_multiple_times_error.txt");
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) Test(org.junit.Test)

Example 14 with ConfigurationException

use of com.google.inject.ConfigurationException in project guice by google.

the class ChildBindingAlreadySetErrorTest method childBindingAlreadySetByJustInTimeBinding.

@Test
public void childBindingAlreadySetByJustInTimeBinding() throws Exception {
    Injector injector = Guice.createInjector(new ChildModule3(), new ChildModule4());
    ConfigurationException exception = assertThrows(ConfigurationException.class, () -> injector.getInstance(DependsOnFoo.class));
    assertGuiceErrorEqualsIgnoreLineNumber(exception.getMessage(), "child_binding_already_set_by_just_in_time_binding.txt");
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) Test(org.junit.Test)

Example 15 with ConfigurationException

use of com.google.inject.ConfigurationException in project guice by google.

the class InjectionPointTest method testTooManyConstructors.

public void testTooManyConstructors() {
    ConfigurationException exception = assertThrows(ConfigurationException.class, () -> InjectionPoint.forConstructorOf(TypeLiteral.get(TooManyConstructors.class)));
    assertThat(exception).hasMessageThat().contains("has more than one constructor annotated with @Inject.");
}
Also used : ConfigurationException(com.google.inject.ConfigurationException)

Aggregations

ConfigurationException (com.google.inject.ConfigurationException)62 Injector (com.google.inject.Injector)16 Errors (com.google.inject.internal.Errors)13 InjectionPoint (com.google.inject.spi.InjectionPoint)8 Method (java.lang.reflect.Method)7 AbstractModule (com.google.inject.AbstractModule)5 Inject (com.google.inject.Inject)5 ErrorsException (com.google.inject.internal.ErrorsException)5 Annotation (java.lang.annotation.Annotation)5 TypeLiteral (com.google.inject.TypeLiteral)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GuiceUtil (com.google.gwt.inject.rebind.util.GuiceUtil)3 Module (com.google.inject.Module)3 Message (com.google.inject.spi.Message)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Binding (com.google.inject.Binding)2