Skip to content

Commit

Permalink
Do not inject String.class without explicit binging (#5)
Browse files Browse the repository at this point in the history
 Do not inject String without explicit binding to fix parametrized tests.
  • Loading branch information
SergeyAlekseevN authored and JeffFaer committed Feb 28, 2019
1 parent 449d119 commit 89fa6cb
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 22 deletions.
6 changes: 6 additions & 0 deletions class-argument-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion disable-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions extension-testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down
13 changes: 12 additions & 1 deletion guice-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,21 @@
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
<version>4.2.2</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ public boolean supportsParameter(ParameterContext parameterContext,
parameter);
Optional<Injector> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> 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) {
}
}
}
6 changes: 6 additions & 0 deletions mockito-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
35 changes: 15 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.jupiter.version>5.4.0</junit.jupiter.version>
<junit.platform.version>1.4.0</junit.platform.version>
<java.version>1.8</java.version>
<base.url>https://github.com/JeffreyFalgout/junit5-extensions</base.url>
</properties>
Expand Down Expand Up @@ -104,19 +104,19 @@
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.6</version>
<version>1.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.6</version>
<version>1.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0-rc3</version>
<version>1.0-rc4</version>
<scope>provided</scope>
</dependency>

Expand All @@ -127,6 +127,13 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand All @@ -148,31 +155,19 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<version>3.1.1</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down

0 comments on commit 89fa6cb

Please sign in to comment.