use of com.google.inject.AbstractModule in project guice by google.
the class ManyConstructorsTest method testNoMatchingConstructorsBecauseTooManyParams.
public void testNoMatchingConstructorsBecauseTooManyParams() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(ComplexFactory.class));
}
});
fail("should have failed");
} catch (CreationException expected) {
Asserts.assertContains(expected.getMessage(), "1) " + Foo.class.getName() + " has @AssistedInject constructors, but none of them match the parameters in method " + ComplexFactory.class.getName());
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class ManyConstructorsTest method testTwoConstructors.
public void testTwoConstructors() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(Factory.class));
}
});
Factory factory = injector.getInstance(Factory.class);
Foo noIndex = factory.create("no index");
assertEquals("no index", noIndex.name);
assertNull(noIndex.index);
Foo index = factory.create("index", 1);
assertEquals("index", index.name);
assertEquals(1, index.index.intValue());
}
use of com.google.inject.AbstractModule in project guice by google.
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.AbstractModule in project guice by google.
the class FactoryProvider2Test method testConstructorExceptionsAreThrownByFactory.
public void testConstructorExceptionsAreThrownByFactory() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(CorrectDefectiveCarFactory.class).toProvider(FactoryProvider.newFactory(CorrectDefectiveCarFactory.class, DefectiveCar.class));
}
});
try {
injector.getInstance(CorrectDefectiveCarFactory.class).createCar();
fail();
} catch (FireException e) {
fail();
} catch (ExplosionException expected) {
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProvider2Test method testFactoryWithImplicitBindings.
public void testFactoryWithImplicitBindings() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Fiat.class));
}
});
ColoredCarFactory coloredCarFactory = injector.getInstance(ColoredCarFactory.class);
Fiat fiat = (Fiat) coloredCarFactory.create(Color.GREEN);
assertEquals(Color.GREEN, fiat.color);
assertNotNull(fiat.steeringWheel);
}
Aggregations