use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testConfigureAnnotatedReturnValue.
public void testConfigureAnnotatedReturnValue() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(Car.class, Names.named("german"), Beetle.class).implement(Car.class, Names.named("american"), Mustang.class).build(AnnotatedVersatileCarFactory.class));
}
});
AnnotatedVersatileCarFactory factory = injector.getInstance(AnnotatedVersatileCarFactory.class);
assertTrue(factory.getGermanCar(Color.GRAY) instanceof Beetle);
assertTrue(factory.getAmericanCar(Color.BLACK) instanceof Mustang);
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testExplicitForwardingAssistedBindingFailsWithInterface.
public void testExplicitForwardingAssistedBindingFailsWithInterface() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Volkswagen.class).to(Golf.class);
install(new FactoryModuleBuilder().implement(Car.class, Volkswagen.class).build(ColoredCarFactory.class));
}
});
fail();
} catch (CreationException ce) {
assertContains(ce.getMessage(), "1) " + Volkswagen.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Volkswagen.class.getName(), "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create(");
assertEquals(1, ce.getErrorMessages().size());
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testAnnotatedAndParentBoundReturnValue.
public void testAnnotatedAndParentBoundReturnValue() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Car.class).to(Golf.class);
bind(Integer.class).toInstance(911);
bind(Double.class).toInstance(5.0d);
install(new FactoryModuleBuilder().implement(Car.class, Names.named("german"), Beetle.class).implement(Car.class, Names.named("american"), Mustang.class).build(AnnotatedVersatileCarFactory.class));
}
});
AnnotatedVersatileCarFactory factory = injector.getInstance(AnnotatedVersatileCarFactory.class);
assertTrue(factory.getGermanCar(Color.BLACK) instanceof Beetle);
assertTrue(injector.getInstance(Car.class) instanceof Golf);
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testParentBoundReturnValue.
public void testParentBoundReturnValue() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Car.class).to(Golf.class);
bind(Double.class).toInstance(5.0d);
install(new FactoryModuleBuilder().implement(Car.class, Mustang.class).build(ColoredCarFactory.class));
}
});
ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
assertTrue(factory.create(Color.RED) instanceof Mustang);
assertTrue(injector.getInstance(Car.class) instanceof Golf);
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testNoBindingAssistedInject.
public void testNoBindingAssistedInject() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(MustangFactory.class));
}
});
MustangFactory factory = injector.getInstance(MustangFactory.class);
Mustang mustang = factory.create(Color.BLUE);
assertEquals(Color.BLUE, mustang.color);
}
Aggregations