|
| 1 | +package com.google.inject.errors; |
| 2 | + |
| 3 | +import static com.google.inject.errors.ErrorMessageTestUtils.assertGuiceErrorEqualsIgnoreLineNumber; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | +import static org.junit.Assume.assumeTrue; |
| 6 | + |
| 7 | +import com.google.inject.AbstractModule; |
| 8 | +import com.google.inject.Guice; |
| 9 | +import com.google.inject.Injector; |
| 10 | +import com.google.inject.Module; |
| 11 | +import com.google.inject.Provides; |
| 12 | +import com.google.inject.ProvisionException; |
| 13 | +import com.google.inject.internal.InternalFlags; |
| 14 | +import com.google.inject.internal.InternalFlags.IncludeStackTraceOption; |
| 15 | +import java.lang.annotation.Retention; |
| 16 | +import java.lang.annotation.RetentionPolicy; |
| 17 | +import java.lang.reflect.Field; |
| 18 | +import javax.inject.Inject; |
| 19 | +import javax.inject.Qualifier; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.junit.runners.JUnit4; |
| 24 | + |
| 25 | +@RunWith(JUnit4.class) |
| 26 | +public final class NullInjectedIntoNonNullableTest { |
| 27 | + |
| 28 | + @Qualifier |
| 29 | + @Retention(RetentionPolicy.RUNTIME) |
| 30 | + @interface Bar {} |
| 31 | + |
| 32 | + static class Foo { |
| 33 | + @Inject |
| 34 | + Foo(@Bar String string) {} |
| 35 | + } |
| 36 | + |
| 37 | + static class FromProvidesMethodModule extends AbstractModule { |
| 38 | + @Provides |
| 39 | + @Bar |
| 40 | + String provideString() { |
| 41 | + return null; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + static class FromProviderModule extends AbstractModule { |
| 46 | + @Override |
| 47 | + protected void configure() { |
| 48 | + bind(String.class).annotatedWith(Bar.class).toProvider(() -> null); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + static class IntermediateModule extends AbstractModule { |
| 53 | + public static final String NULL = null; |
| 54 | + |
| 55 | + private static final Module MODULE = |
| 56 | + new AbstractModule() { |
| 57 | + @Override |
| 58 | + protected void configure() { |
| 59 | + Field field = null; |
| 60 | + try { |
| 61 | + field = IntermediateModule.class.getField("NULL"); |
| 62 | + } catch (NoSuchFieldException error) { |
| 63 | + throw new AssertionError("NULL field missing!"); |
| 64 | + } |
| 65 | + binder() |
| 66 | + .withSource(field) |
| 67 | + .bind(String.class) |
| 68 | + .annotatedWith(Bar.class) |
| 69 | + .toProvider(() -> NULL); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void configure() { |
| 75 | + install(MODULE); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Before |
| 80 | + public void ensureStackTraceIsIncluded() { |
| 81 | + assumeTrue(InternalFlags.getIncludeStackTraceOption() != IncludeStackTraceOption.OFF); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void nullReturnedFromProvidesMethod() { |
| 86 | + Injector injector = Guice.createInjector(new FromProvidesMethodModule()); |
| 87 | + |
| 88 | + ProvisionException exception = |
| 89 | + assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class)); |
| 90 | + assertGuiceErrorEqualsIgnoreLineNumber( |
| 91 | + exception.getMessage(), "null_returned_from_provides_method.txt"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void nullReturnedFromProvider() { |
| 96 | + Injector injector = Guice.createInjector(new FromProviderModule()); |
| 97 | + |
| 98 | + ProvisionException exception = |
| 99 | + assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class)); |
| 100 | + assertGuiceErrorEqualsIgnoreLineNumber( |
| 101 | + exception.getMessage(), "null_returned_from_provider.txt"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void nullReturnedFromProviderWithModuleStack() { |
| 106 | + Injector injector = Guice.createInjector(new IntermediateModule()); |
| 107 | + |
| 108 | + ProvisionException exception = |
| 109 | + assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class)); |
| 110 | + assertGuiceErrorEqualsIgnoreLineNumber( |
| 111 | + exception.getMessage(), "null_returned_from_provider_with_module_stack.txt"); |
| 112 | + } |
| 113 | +} |
0 commit comments