use of com.google.inject.CreationException in project roboguice by roboguice.
the class FactoryProvider2Test method testFactoryFailsWithMissingBinding.
public void testFactoryFailsWithMissingBinding() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "Could not find a suitable constructor in java.lang.Double.", "at " + ColoredCarFactory.class.getName() + ".create(FactoryProvider2Test.java");
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class FactoryProvider2Test method testAssistedProviderIsDisallowed.
public void testAssistedProviderIsDisallowed() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(ProviderBasedColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ProviderBasedColoredCarFactory.class, Subaru.class));
}
});
fail();
} catch (CreationException expected) {
assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
// Assert each method individually, because JDK7 doesn't guarantee method ordering.
assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [2] with key" + " [com.google.inject.Provider<java.lang.String>] on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">" + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + " on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createMustang()]");
assertContains(expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$ProviderBasedColoredCarFactory was bound.");
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class OverrideModuleTest method testFailsIfOverridenScopeInstanceHasBeenUsed.
public void testFailsIfOverridenScopeInstanceHasBeenUsed() {
final Scope scope = new Scope() {
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return unscoped;
}
@Override
public String toString() {
return "ORIGINAL SCOPE";
}
};
final Module original = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(Date.class).in(scope);
bind(String.class).in(scope);
}
};
Module originalWrapper = new AbstractModule() {
@Override
protected void configure() {
install(original);
}
};
Module replacements = new AbstractModule() {
@Override
protected void configure() {
bindScope(TestScopeAnnotation.class, new SingleUseScope());
}
};
try {
createInjector(Modules.override(originalWrapper).with(replacements));
fail("Exception expected");
} catch (CreationException e) {
assertContains(e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "at ", replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass()));
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class CheckedProviderTest method testManyMethods.
public void testManyMethods() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(ThrowingProviderBinder.forModule(this));
}
@SuppressWarnings("unused")
@CheckedProvides(ManyMethods.class)
String foo() {
return null;
}
});
fail();
} catch (CreationException ce) {
assertEquals(ManyMethods.class.getName() + " may not declare any new methods, but declared " + Arrays.asList(ManyMethods.class.getDeclaredMethods()), Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class CheckedProviderTest method testCxtorWithSuperclassExceptionFails.
public void testCxtorWithSuperclassExceptionFails() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(SuperclassExceptionFoo.class);
}
});
fail();
} catch (CreationException ce) {
assertEquals(IOException.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());
}
}
Aggregations