Search in sources :

Example 36 with CreationException

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

the class ThrowingProviderTest method testIncorrectPredefinedType_Provides.

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

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(StringRemoteProvider.class)
            Integer foo() {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(StringRemoteProvider.class.getName() + " expects the value type to be java.lang.String, but it was java.lang.Integer", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 37 with CreationException

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

the class ThrowingProviderTest method testProviderMethodWithSuperclassFails.

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

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(RemoteProvider.class)
            String foo() throws IOException {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(IOException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AbstractModule(com.google.inject.AbstractModule)

Example 38 with CreationException

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

the class ThrowingProviderTest method testIncorrectPredefinedType_Bind.

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

            @Override
            protected void configure() {
                ThrowingProviderBinder.create(binder()).bind(StringRemoteProvider.class, Integer.class).to(new StringRemoteProvider() {

                    @Override
                    public String get() throws RemoteException {
                        return "A";
                    }
                });
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(StringRemoteProvider.class.getName() + " expects the value type to be java.lang.String, but it was java.lang.Integer", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 39 with CreationException

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

the class BinderTest method testDanglingConstantBinding.

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

            @Override
            public void configure() {
                bindConstant();
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Missing constant value. Please call to(...).", "at " + getClass().getName());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 40 with CreationException

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

the class BinderTest method testBindingNullConstant.

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

            @Override
            public void configure() {
                String none = null;
                bindConstant().annotatedWith(Names.named("nullOne")).to(none);
                bind(String.class).annotatedWith(Names.named("nullTwo")).toInstance(none);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding to null instances is not allowed. Use toProvider(Providers.of(null))", "2) Binding to null instances is not allowed. Use toProvider(Providers.of(null))");
    }
}
Also used : CreationException(com.google.inject.CreationException) 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