use of com.google.inject.CreationException in project roboguice by roboguice.
the class ManyConstructorsTest method testTooManyMatchingConstructors.
public void testTooManyMatchingConstructors() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(Foo.class, TooManyMatches.class).build(SimpleFactory2.class));
}
});
fail("should have failed");
} catch (CreationException expected) {
Asserts.assertContains(expected.getMessage(), "1) " + TooManyMatches.class.getName() + " has more than one constructor annotated with @AssistedInject that " + "matches the parameters in method " + SimpleFactory2.class.getName());
}
}
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());
}
}
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))");
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testRecursiveBinding.
public void testRecursiveBinding() {
try {
Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
bind(Runnable.class).to(Runnable.class);
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) Binding points to itself.", "at " + getClass().getName(), getDeclaringSourcePart(getClass()));
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testArrayTypeCanonicalization.
/**
* Although {@code String[].class} isn't equal to {@code new
* GenericArrayTypeImpl(String.class)}, Guice should treat these two types
* interchangeably.
*/
public void testArrayTypeCanonicalization() {
final String[] strings = new String[] { "A" };
final Integer[] integers = new Integer[] { 1 };
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String[].class).toInstance(strings);
bind(new TypeLiteral<Integer[]>() {
}).toInstance(integers);
}
});
assertSame(integers, injector.getInstance(Key.get(new TypeLiteral<Integer[]>() {
})));
assertSame(integers, injector.getInstance(new Key<Integer[]>() {
}));
assertSame(integers, injector.getInstance(Integer[].class));
assertSame(strings, injector.getInstance(Key.get(new TypeLiteral<String[]>() {
})));
assertSame(strings, injector.getInstance(new Key<String[]>() {
}));
assertSame(strings, injector.getInstance(String[].class));
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String[].class).toInstance(new String[] { "A" });
bind(new TypeLiteral<String[]>() {
}).toInstance(new String[] { "B" });
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) A binding to java.lang.String[] was already configured at " + getClass().getName(), "at " + getClass().getName(), getDeclaringSourcePart(getClass()));
assertContains(expected.getMessage(), "1 error");
}
// passes because duplicates are ignored
injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String[].class).toInstance(strings);
bind(new TypeLiteral<String[]>() {
}).toInstance(strings);
}
});
assertSame(strings, injector.getInstance(Key.get(new TypeLiteral<String[]>() {
})));
assertSame(strings, injector.getInstance(new Key<String[]>() {
}));
assertSame(strings, injector.getInstance(String[].class));
}
Aggregations