use of com.google.inject.CreationException in project roboguice by roboguice.
the class CheckedProviderTest method testProviderMethodWithWrongException.
public void testProviderMethodWithWrongException() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(ThrowingProviderBinder.forModule(this));
}
@SuppressWarnings("unused")
@CheckedProvides(RemoteProvider.class)
String foo() throws InterruptedException {
return null;
}
});
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());
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class CheckedProviderTest method testBindingToNonInterfaceType_Provides.
public void testBindingToNonInterfaceType_Provides() throws Exception {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(ThrowingProviderBinder.forModule(this));
}
@SuppressWarnings("unused")
@CheckedProvides(MockRemoteProvider.class)
Foo foo() {
return null;
}
});
fail();
} catch (CreationException expected) {
assertEquals(MockRemoteProvider.class.getName() + " must be an interface", Iterables.getOnlyElement(expected.getErrorMessages()).getMessage());
}
}
use of com.google.inject.CreationException in project guice by google.
the class OptionalBinderTest method testDifferentBindingsFail_both.
public void testDifferentBindingsFail_both() {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("a");
OptionalBinder.newOptionalBinder(binder(), String.class).setDefault().toInstance("b");
OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("b");
OptionalBinder.newOptionalBinder(binder(), String.class).setBinding().toInstance("c");
}
};
try {
Guice.createInjector(module);
fail();
} catch (CreationException ce) {
assertEquals(ce.getMessage(), 2, 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(", "2) 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(");
}
}
use of com.google.inject.CreationException in project guice by google.
the class OptionalBinderTest method testTypeNotBoundByDefault.
public void testTypeNotBoundByDefault() {
Module module = new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), String.class);
// the above specifies this.
requireBinding(new Key<Optional<String>>() {
});
// but it doesn't specify this.
requireBinding(String.class);
// need to do this, otherwise String will JIT
binder().requireExplicitBindings();
if (HAS_JAVA_OPTIONAL) {
requireBinding(Key.get(javaOptionalOfString));
}
}
};
try {
Guice.createInjector(module);
fail();
} catch (CreationException ce) {
assertContains(ce.getMessage(), "1) Explicit bindings are required and java.lang.String is not explicitly bound.");
assertEquals(1, ce.getErrorMessages().size());
}
}
use of com.google.inject.CreationException in project guice by google.
the class FactoryModuleBuilderTest method testSimilarBindingsWithConflictingImplementations.
public void testSimilarBindingsWithConflictingImplementations() {
try {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(Car.class, Mustang.class).build(ColoredCarFactory.class));
install(new FactoryModuleBuilder().implement(Car.class, Golf.class).build(ColoredCarFactory.class));
}
});
injector.getInstance(ColoredCarFactory.class);
fail();
} catch (CreationException ce) {
assertContains(ce.getMessage(), "A binding to " + ColoredCarFactory.class.getName() + " was already configured");
assertEquals(1, ce.getErrorMessages().size());
}
}
Aggregations