Search in sources :

Example 11 with CreationException

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

the class BinderTest method testUserReportedErrorsAreAlsoLogged.

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

            @Override
            protected void configure() {
                addError(new Message("Whoops!", new IllegalArgumentException()));
            }
        });
        fail();
    } catch (CreationException expected) {
    }
    LogRecord logRecord = Iterables.getOnlyElement(this.logRecords);
    assertContains(logRecord.getMessage(), "An exception was caught and reported. Message: java.lang.IllegalArgumentException");
}
Also used : Message(com.google.inject.spi.Message) LogRecord(java.util.logging.LogRecord) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 12 with CreationException

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

the class BinderTest method testBindingToProvider.

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

            @Override
            protected void configure() {
                bind(new TypeLiteral<Provider<String>>() {
                }).toInstance(Providers.of("A"));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 13 with CreationException

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

the class BinderTest method testToStringOnBinderApi.

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

            @Override
            public void configure() {
                assertEquals("Binder", binder().toString());
                assertEquals("Provider<java.lang.Integer>", getProvider(Integer.class).toString());
                assertEquals("Provider<java.util.List<java.lang.String>>", getProvider(Key.get(new TypeLiteral<List<String>>() {
                })).toString());
                assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).toString());
                assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).annotatedWith(Names.named("a")).toString());
                assertEquals("ConstantBindingBuilder", bindConstant().toString());
                assertEquals("ConstantBindingBuilder", bindConstant().annotatedWith(Names.named("b")).toString());
                assertEquals("AnnotatedElementBuilder", binder().newPrivateBinder().expose(Integer.class).toString());
            }
        });
        fail();
    } catch (CreationException ignored) {
    }
}
Also used : List(java.util.List) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 14 with CreationException

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

the class CheckedProviderTest method testEarlyBindingError.

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

            @Override
            protected void configure() {
                ThrowingProviderBinder.create(binder()).bind(StringRemoteProvider.class, String.class).to(FailingProvider.class);
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals("Could not find a suitable constructor in " + FailingProvider.class.getName() + ". Classes must have either one (and only one) constructor annotated with @Inject" + " or a zero-argument constructor that is not private.", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 15 with CreationException

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

the class CheckedProviderTest method testCxtorWithWrongException.

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

            @Override
            protected void configure() {
                ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(WrongExceptionFoo.class);
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : BindException(java.net.BindException) CreationException(com.google.inject.CreationException) RemoteException(java.rmi.RemoteException) 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