use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testSingleton.
public void testSingleton() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class).in(Singleton.class);
}
});
B b = injector.getInstance(B.class);
assertSame(b, injector.getInstance(B.class));
assertSame(b, injector.getInstance(B.class));
J j = injector.getInstance(J.class);
assertSame(j, injector.getInstance(J.class));
assertSame(j, injector.getInstance(J.class));
}
use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testInjectingFinalFieldsIsForbidden.
public void testInjectingFinalFieldsIsForbidden() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(L.class);
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) Injected field " + L.class.getName() + ".b cannot be final.");
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testInject.
public void testInject() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class).toInstance(b);
bind(C.class).toInstance(c);
bind(D.class).toInstance(d);
bind(E.class).toInstance(e);
bind(A.class);
}
});
A a = injector.getInstance(A.class);
assertSame(b, a.b);
assertSame(c, a.c);
assertSame(d, a.d);
assertSame(e, a.e);
}
use of com.google.inject.AbstractModule in project guice by google.
the class FactoryProvider2Test method testMethodsDeclaredInObject.
public void testMethodsDeclaredInObject() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Double.class).toInstance(5.0d);
bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
}
});
ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
assertEqualsBothWays(carFactory, carFactory);
}
use of com.google.inject.AbstractModule in project guice by google.
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.");
}
}
Aggregations