From 89fa6cb1e781f663e780f746ca82076fa4a440f6 Mon Sep 17 00:00:00 2001 From: Sergey Alekseev Date: Thu, 28 Feb 2019 03:51:10 +0300 Subject: [PATCH] Do not inject String.class without explicit binging (#5) Do not inject String without explicit binding to fix parametrized tests. --- class-argument-converter/pom.xml | 6 ++ disable-extension/pom.xml | 7 +- extension-testing/pom.xml | 6 ++ guice-extension/pom.xml | 13 ++- .../testing/junit/guice/GuiceExtension.java | 6 ++ .../ParametrizedTestCompatibilityTest.java | 99 +++++++++++++++++++ mockito-extension/pom.xml | 6 ++ pom.xml | 35 +++---- 8 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 guice-extension/src/test/java/name/falgout/jeffrey/testing/junit/guice/ParametrizedTestCompatibilityTest.java diff --git a/class-argument-converter/pom.xml b/class-argument-converter/pom.xml index 8027de2..f54237f 100644 --- a/class-argument-converter/pom.xml +++ b/class-argument-converter/pom.xml @@ -40,6 +40,12 @@ junit-jupiter-params compile + + org.junit.jupiter + junit-jupiter-engine + test + + com.google.guava guava diff --git a/disable-extension/pom.xml b/disable-extension/pom.xml index b7f2302..ea7c26a 100644 --- a/disable-extension/pom.xml +++ b/disable-extension/pom.xml @@ -39,11 +39,16 @@ org.junit.platform junit-platform-commons - org.junit.jupiter junit-jupiter-params + + org.junit.jupiter + junit-jupiter-engine + test + + com.google.truth truth diff --git a/extension-testing/pom.xml b/extension-testing/pom.xml index 943c2ac..81ce52d 100644 --- a/extension-testing/pom.xml +++ b/extension-testing/pom.xml @@ -39,6 +39,12 @@ junit-platform-launcher ${junit.platform.version} + + org.junit.jupiter + junit-jupiter-engine + test + + com.google.truth truth diff --git a/guice-extension/pom.xml b/guice-extension/pom.xml index f1f3b54..0c8836e 100644 --- a/guice-extension/pom.xml +++ b/guice-extension/pom.xml @@ -39,10 +39,21 @@ org.junit.platform junit-platform-commons + + org.junit.jupiter + junit-jupiter-params + test + + + org.junit.jupiter + junit-jupiter-engine + test + + com.google.inject guice - 4.1.0 + 4.2.2 diff --git a/guice-extension/src/main/java/name/falgout/jeffrey/testing/junit/guice/GuiceExtension.java b/guice-extension/src/main/java/name/falgout/jeffrey/testing/junit/guice/GuiceExtension.java index a6c0f1f..ebdec67 100644 --- a/guice-extension/src/main/java/name/falgout/jeffrey/testing/junit/guice/GuiceExtension.java +++ b/guice-extension/src/main/java/name/falgout/jeffrey/testing/junit/guice/GuiceExtension.java @@ -220,6 +220,12 @@ public boolean supportsParameter(ParameterContext parameterContext, parameter); Optional optInjector = getInjectorForParameterResolution(extensionContext); return optInjector.filter(injector -> { + + // Do not bind String without explicit bindings. + if (key.equals(Key.get(String.class)) && injector.getExistingBinding(key) == null) { + return false; + } + try { injector.getInstance(key); return true; diff --git a/guice-extension/src/test/java/name/falgout/jeffrey/testing/junit/guice/ParametrizedTestCompatibilityTest.java b/guice-extension/src/test/java/name/falgout/jeffrey/testing/junit/guice/ParametrizedTestCompatibilityTest.java new file mode 100644 index 0000000..7a09533 --- /dev/null +++ b/guice-extension/src/test/java/name/falgout/jeffrey/testing/junit/guice/ParametrizedTestCompatibilityTest.java @@ -0,0 +1,99 @@ +package name.falgout.jeffrey.testing.junit.guice; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; + +import com.google.inject.AbstractModule; +import com.google.inject.name.Named; +import com.google.inject.name.Names; +import name.falgout.jeffrey.testing.junit.testing.ExpectFailure; +import name.falgout.jeffrey.testing.junit.testing.ExpectFailure.Cause; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + + +public class ParametrizedTestCompatibilityTest { + static final class TestModule extends AbstractModule { + static final String STRING = "testModuleString"; + + @Override + protected void configure() { + bind(String.class).toInstance(STRING); + bind(String.class).annotatedWith(Names.named("named")).toInstance(STRING); + bind(String.class).annotatedWith(SomeBindingAnnotation.class).toInstance(STRING); + bind(String.class).annotatedWith(SomeQualifyingAnnotation.class).toInstance(STRING); + } + } + + @Nested + class PositiveCases { + + @ExtendWith(GuiceExtension.class) + @ParameterizedTest + @ValueSource(strings = "valueSourceString") + void parametrizedTestWithStringsShouldWorkWithGuiceExtension(String value) { + assertEquals("valueSourceString", value); + } + + @ExtendWith(GuiceExtension.class) + @ParameterizedTest + @ValueSource(classes = Integer.class) + void parametrizedTestWithoutStringsShouldWorkWithGuiceExtension(Class clazz) { + assertSame(Integer.class, clazz); + } + } + + @Nested + class NegativeCases { + @ExpectFailure( + @Cause( + type = ParameterResolutionException.class, + message = "Discovered multiple competing ParameterResolvers" + ) + ) + @IncludeModule(TestModule.class) + @ParameterizedTest + @ValueSource(strings = "valueSourceString") + void explicitBindingStringShouldConflictWithValueSource(String value) { + } + + @ExpectFailure( + @Cause( + type = ParameterResolutionException.class, + message = "Discovered multiple competing ParameterResolvers" + ) + ) + @IncludeModule(TestModule.class) + @ParameterizedTest + @ValueSource(strings = "valueSourceString") + void explicitBindingStringShouldConflictWithValueSource2(@SomeBindingAnnotation String value) { + } + + @ExpectFailure( + @Cause( + type = ParameterResolutionException.class, + message = "Discovered multiple competing ParameterResolvers" + ) + ) + @IncludeModule(TestModule.class) + @ParameterizedTest + @ValueSource(strings = "valueSourceString") + void explicitBindingStringShouldConflictWithValueSource3(@SomeQualifyingAnnotation String value) { + } + + @ExpectFailure( + @Cause( + type = ParameterResolutionException.class, + message = "Discovered multiple competing ParameterResolvers" + ) + ) + @IncludeModule(TestModule.class) + @ParameterizedTest + @ValueSource(strings = "valueSourceString") + void explicitBindingStringShouldConflictWithValueSource4(@Named("named") String value) { + } + } +} diff --git a/mockito-extension/pom.xml b/mockito-extension/pom.xml index 8832614..63ac0fb 100644 --- a/mockito-extension/pom.xml +++ b/mockito-extension/pom.xml @@ -34,6 +34,12 @@ org.junit.jupiter junit-jupiter-api + + org.junit.jupiter + junit-jupiter-engine + test + + com.google.guava guava diff --git a/pom.xml b/pom.xml index 3dbf19f..1ee885d 100644 --- a/pom.xml +++ b/pom.xml @@ -17,8 +17,8 @@ UTF-8 - 5.0.0 - 1.0.0 + 5.4.0 + 1.4.0 1.8 https://github.com/JeffreyFalgout/junit5-extensions @@ -104,19 +104,19 @@ com.google.auto.value auto-value - 1.6 + 1.6.3 provided com.google.auto.value auto-value-annotations - 1.6 + 1.6.3 provided com.google.auto.service auto-service - 1.0-rc3 + 1.0-rc4 provided @@ -127,6 +127,13 @@ ${junit.jupiter.version} test + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + com.google.truth truth @@ -148,7 +155,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.6.1 + 3.8.0 ${java.version} ${java.version} @@ -156,23 +163,11 @@ maven-surefire-plugin - 2.20 - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - - - org.junit.jupiter - junit-jupiter-engine - ${junit.jupiter.version} - - + 2.22.1 maven-jar-plugin - 2.4 + 3.1.1