Search in sources :

Example 51 with CreationException

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

the class OptionalBinderTest method testSourceLinesInException.

public void testSourceLinesInException() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                OptionalBinder.newOptionalBinder(binder(), Integer.class).setDefault();
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "No implementation for java.lang.Integer", "at " + getClass().getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 52 with CreationException

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

the class OptionalBinderTest method testDifferentBindingsFail_defaults.

public void testDifferentBindingsFail_defaults() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("a");
            OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("b");
        }
    };
    try {
        Guice.createInjector(module);
        fail();
    } catch (CreationException ce) {
        assertEquals(ce.getMessage(), 1, ce.getErrorMessages().size());
        assertContains(ce.getMessage(), "1) A binding to java.lang.String annotated with @" + RealOptionalBinder.Default.class.getName() + " was already configured at " + module.getClass().getName() + ".configure(", "at " + module.getClass().getName() + ".configure(");
    }
}
Also used : CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 53 with CreationException

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

the class MapBinderTest method testMapBinderMapForbidsDuplicateKeys.

public void testMapBinderMapForbidsDuplicateKeys() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class);
            multibinder.addBinding("a").toInstance("A");
            multibinder.addBinding("a").toInstance("B");
        }
    };
    try {
        Guice.createInjector(module);
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "Map injection failed due to duplicated key \"a\"");
    }
    assertMapVisitor(Key.get(mapOfString), stringType, stringType, setOf(module), MODULE, false, 0, instance("a", "A"), instance("a", "B"));
}
Also used : CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 54 with CreationException

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

the class MultibinderTest method testSourceLinesInMultibindings.

public void testSourceLinesInMultibindings() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                Multibinder.newSetBinder(binder(), Integer.class).addBinding();
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), true, "No implementation for java.lang.Integer", "at " + getClass().getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 55 with CreationException

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

the class OptionalBinderTest method testDifferentBindingsFail_actual.

public void testDifferentBindingsFail_actual() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("a");
            OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("b");
        }
    };
    try {
        Guice.createInjector(module);
        fail();
    } catch (CreationException ce) {
        assertEquals(ce.getMessage(), 1, ce.getErrorMessages().size());
        assertContains(ce.getMessage(), "1) A binding to java.lang.String annotated with @" + RealOptionalBinder.Actual.class.getName() + " was already configured at " + module.getClass().getName() + ".configure(", "at " + module.getClass().getName() + ".configure(");
    }
}
Also used : CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Aggregations

CreationException (com.google.inject.CreationException)169 AbstractModule (com.google.inject.AbstractModule)163 Module (com.google.inject.Module)26 RemoteException (java.rmi.RemoteException)18 BindException (java.net.BindException)12 IOException (java.io.IOException)9 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)7 PrivateModule (com.google.inject.PrivateModule)7 TooManyListenersException (java.util.TooManyListenersException)6 Provider (com.google.inject.Provider)5 Message (com.google.inject.spi.Message)5 ArrayList (java.util.ArrayList)5 Injector (com.google.inject.Injector)4 Provides (com.google.inject.Provides)4 AccessException (java.rmi.AccessException)4 Key (com.google.inject.Key)3 TypeLiteral (com.google.inject.TypeLiteral)3 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2