Skip to content

Commit 12d8a63

Browse files
committed
Do not greedily inject @ParameterizedTest; instead treat is as explicit param. injection
1 parent 7c79b90 commit 12d8a63

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

junit5/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ This default behaviour includes:
8989
* Inject into method parameters of your test methods
9090
* If the type of the parameter matches a known and resolvable bean
9191
* By default, Weld is greedy and will try to resolve all parameters which are known as bean types in the container
92+
* An exception to this rule is `@ParameterizedTest` where Weld requires explicitly stating CDI qualifiers for each method parameter which should be injected
9293
* If this behaviour should be different, refer to [additional configuration section](#explicit-parameter-injection)
9394
* Shut down the container after test is done
9495

junit5/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<groupId>org.junit.jupiter</groupId>
3232
<artifactId>junit-jupiter-engine</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-params</artifactId>
37+
</dependency>
3438

3539
<!-- Test dependencies -->
3640
<dependency>

junit5/src/main/java/org/jboss/weld/junit5/WeldJunit5Extension.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.junit.jupiter.api.extension.ParameterResolutionException;
5959
import org.junit.jupiter.api.extension.ParameterResolver;
6060
import org.junit.jupiter.api.io.TempDir;
61+
import org.junit.jupiter.params.ParameterizedTest;
6162

6263
/**
6364
* JUnit 5 extension allowing to bootstrap Weld SE container for each @Test method (or once per test class
@@ -192,7 +193,10 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
192193
List<Annotation> qualifiers = resolveQualifiers(parameterContext,
193194
getContainerFromStore(extensionContext).getBeanManager());
194195
// if we require explicit parameter injection (via global settings or annotation) and there are no qualifiers we don't resolve it
195-
if ((getExplicitInjectionInfoFromStore(extensionContext) || (methodRequiresExplicitParamInjection(parameterContext)))
196+
// if the method is annotated @ParameterizedTest, we treat it as explicit param injection and require qualifiers
197+
if ((getExplicitInjectionInfoFromStore(extensionContext)
198+
|| methodRequiresExplicitParamInjection(parameterContext)
199+
|| methodIsParameterizedTest(parameterContext))
196200
&& qualifiers.isEmpty()) {
197201
return false;
198202
} else {
@@ -253,6 +257,10 @@ private boolean methodRequiresExplicitParamInjection(ParameterContext pc) {
253257
return false;
254258
}
255259

260+
private boolean methodIsParameterizedTest(ParameterContext pc) {
261+
return pc.getDeclaringExecutable().getAnnotation(ParameterizedTest.class) != null ? true : false;
262+
}
263+
256264
private TestInstance.Lifecycle determineTestLifecycle(ExtensionContext ec) {
257265
// check the test for org.junit.jupiter.api.TestInstance annotation
258266
TestInstance annotation = ec.getRequiredTestClass().getAnnotation(TestInstance.class);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.jboss.weld.junit5.explicitInjection.parameterizedTest;
2+
3+
import jakarta.enterprise.context.ApplicationScoped;
4+
import jakarta.enterprise.inject.Produces;
5+
6+
@ApplicationScoped
7+
public class Foo {
8+
9+
@Produces
10+
String s = "fooString";
11+
12+
String ping() {
13+
return Foo.class.getSimpleName();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.jboss.weld.junit5.explicitInjection.parameterizedTest;
2+
3+
import java.util.Set;
4+
5+
import jakarta.enterprise.inject.Default;
6+
7+
import org.jboss.weld.junit5.WeldJunit5Extension;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.ValueSource;
12+
13+
@ExtendWith(WeldJunit5Extension.class)
14+
public class ParameterizedTestExplicitInjectionTest {
15+
16+
public static final Set<String> strings = Set.of("one", "two", "three");
17+
18+
@ParameterizedTest
19+
@ValueSource(strings = { "one", "two", "three" })
20+
public void noWeldInjection(String param) {
21+
// String param is not resolved by Weld
22+
Assertions.assertTrue(strings.contains(param));
23+
}
24+
25+
// NOTE: swapping parameters in the below tests leads to a failure! Parameterized test attempts to claim the first
26+
// parameter as its own and cast to given type; there is nothing we can do about that
27+
@ParameterizedTest
28+
@ValueSource(strings = { "one", "two", "three" })
29+
public void parameterizedTestWithWeldInjection(String param, @Default Foo foo) {
30+
// String param is not resolved by Weld
31+
Assertions.assertTrue(strings.contains(param));
32+
// Foo has explicit qualifier and Weld therefore still tried to resolve it
33+
Assertions.assertNotNull(foo);
34+
Assertions.assertEquals(Foo.class.getSimpleName(), foo.ping());
35+
}
36+
37+
@ParameterizedTest
38+
@ValueSource(strings = { "one", "two", "three" })
39+
public void parameterizedTestWithTwoStringParams(String param, @Default String anotherString) {
40+
// String param is not resolved by Weld
41+
Assertions.assertTrue(strings.contains(param));
42+
// Foo has explicit qualifier and Weld therefore still tried to resolve it
43+
Assertions.assertNotNull(anotherString);
44+
Assertions.assertEquals("fooString", anotherString);
45+
}
46+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
<version>${version.junit.jupiter}</version>
8989
</dependency>
9090

91+
<dependency>
92+
<groupId>org.junit.jupiter</groupId>
93+
<artifactId>junit-jupiter-params</artifactId>
94+
<version>${version.junit.jupiter}</version>
95+
</dependency>
96+
9197
<dependency>
9298
<groupId>org.junit.jupiter</groupId>
9399
<artifactId>junit-jupiter-engine</artifactId>

0 commit comments

Comments
 (0)