public void testMultipleBindingAnnotations() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() {} @Provides @Named("A") @Blue public String provideString() { return "a"; } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "more than one annotation annotated with @BindingAnnotation:", "Named", "Blue", "at " + getClass().getName(), ".provideString(ProviderMethodsTest.java:"); } }
public void testFactoryMethodMustDeclareAllConstructorExceptions() { try { FactoryProvider.newFactory(DefectiveCarFactoryWithNoExceptions.class, DefectiveCar.class); fail(); } catch (ConfigurationException expected) { assertContains(expected.getMessage(), "no compatible exception is thrown"); } }
public void testFactoryMethodsMismatch() { try { FactoryProvider.newFactory(SummerCarFactory.class, Beetle.class); fail(); } catch (ConfigurationException e) { assertContains(e.getMessage(), "Constructor mismatch"); } }
public void testProvidesMethodInheritenceHierarchy() { try { Guice.createInjector(new Sub1Module(), new Sub2Module()); fail("Expected injector creation failure"); } catch (CreationException expected) { // both of our super class bindings cause errors assertContains( expected.getMessage(), "A binding to java.lang.Long was already configured", "A binding to java.lang.Integer was already configured"); } }
private void validateNullableFails(Injector injector, Module module) { try { injector.getInstance(Integer.class); fail(); } catch (ProvisionException expected) { assertContains( expected.getMessage(), "1) null returned by binding at " + module.getClass().getName() + ".configure(", "but the 1st parameter of " + module.getClass().getName() + ".fail(", "is not @Nullable", "while locating java.lang.String", "for the 1st parameter of " + module.getClass().getName() + ".fail(", "while locating java.lang.Integer"); assertEquals(1, expected.getErrorMessages().size()); } }
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(), "1) Parameter of type 'double' is not injectable or annotated with @Assisted"); } }
public void testOverrideProviderMethod_covariantOverrideDoesntHaveProvides() { class SubClassModule extends SuperClassModule { @Override Double providerMethod() { return 2D; } } try { Guice.createInjector(new SubClassModule()); fail(); } catch (CreationException e) { assertContains( e.getMessage(), "Overriding @Provides methods is not allowed.", "@Provides method: " + SuperClassModule.class.getName() + ".providerMethod()", "overridden by: " + SubClassModule.class.getName() + ".providerMethod()"); } }
public void testOverrideProviderMethod_superclassRawTypes_returnType() { class SubClassModule extends SuperClassModule { // remove the rawtype from the override @Override List<String> rawProvider(List<String> f) { return f; } } try { Guice.createInjector(new SubClassModule()); fail(); } catch (CreationException e) { assertContains( e.getMessage(), "Overriding @Provides methods is not allowed.", "@Provides method: " + SuperClassModule.class.getName() + ".rawProvider()", "overridden by: " + SubClassModule.class.getName() + ".rawProvider()"); } }
public void testVoidProviderMethods() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() {} @Provides void provideFoo() {} }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "1) Provider methods must return a value. Do not return void.", getClass().getName(), ".provideFoo(ProviderMethodsTest.java:"); } }
public void testOverrideProviderMethod_subclassRawTypes_returnType() { class SubClassModule extends SuperClassModule { @Override List annotatedGenericProviderMethod() { return super.annotatedGenericProviderMethod(); } } try { Guice.createInjector(new SubClassModule()); fail(); } catch (CreationException e) { assertContains( e.getMessage(), "Overriding @Provides methods is not allowed.", "@Provides method: " + SuperClassModule.class.getName() + ".annotatedGenericProviderMethod()", "overridden by: " + SubClassModule.class.getName() + ".annotatedGenericProviderMethod()"); } }
public void testOverrideProviderMethod_overrideHasProvides_withNewAnnotation() { class SubClassModule extends SuperClassModule { @Override @Provides @Named("foo") Number providerMethod() { return 2D; } } try { Guice.createInjector(new SubClassModule()); fail(); } catch (CreationException e) { assertContains( e.getMessage(), "Overriding @Provides methods is not allowed.", "@Provides method: " + SuperClassModule.class.getName() + ".providerMethod()", "overridden by: " + SubClassModule.class.getName() + ".providerMethod()"); } }
public void testAssistedInjectConstructorAndAssistedFactoryParameterMustNotMix() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); bind(AssistedParamsFactory.class) .toProvider( FactoryProvider.newFactory(AssistedParamsFactory.class, Mustang.class)); } }); fail(); } catch (CreationException expected) { assertContains( expected.getMessage(), "Factory method " + AssistedParamsFactory.class.getName() + ".create() has an @Assisted parameter, which " + "is incompatible with the deprecated @AssistedInject annotation."); } }
public void testScopedProviderMethodThrowsException() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() {} @Provides @Singleton int provideInt() { throw new RuntimeException("boom"); } }); Provider<Integer> intProvider = injector.getProvider(Integer.class); try { intProvider.get(); fail(); } catch (ProvisionException pe) { // by default assertContains asserts that the last item doesn't repeat... which is the main // thing we are testing for assertContains(pe.getMessage(), "java.lang.RuntimeException: boom", "provideInt"); } }
// This is a tricky case where signatures don't match, but it is an override (facilitated via a // bridge method) public void testOverrideProviderMethod_erasureBasedOverrides() { class SubClassModule extends GenericSuperModule<Integer> { @Override String provide(Integer thing) { return thing.toString(); } @Override protected void configure() { bind(Integer.class).toInstance(3); } } try { Guice.createInjector(new SubClassModule()); fail(); } catch (CreationException e) { assertContains( e.getMessage(), "Overriding @Provides methods is not allowed.", "@Provides method: " + GenericSuperModule.class.getName() + ".provide()", "overridden by: " + SubClassModule.class.getName() + ".provide()"); } }