Skip to content

Commit

Permalink
Do not greedily inject @ParameterizedTest; instead treat is as explic…
Browse files Browse the repository at this point in the history
…it param. injection
  • Loading branch information
manovotn committed Feb 21, 2024
1 parent 7c79b90 commit 12d8a63
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ This default behaviour includes:
* Inject into method parameters of your test methods
* If the type of the parameter matches a known and resolvable bean
* By default, Weld is greedy and will try to resolve all parameters which are known as bean types in the container
* An exception to this rule is `@ParameterizedTest` where Weld requires explicitly stating CDI qualifiers for each method parameter which should be injected
* If this behaviour should be different, refer to [additional configuration section](#explicit-parameter-injection)
* Shut down the container after test is done

Expand Down
4 changes: 4 additions & 0 deletions junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;

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

private boolean methodIsParameterizedTest(ParameterContext pc) {
return pc.getDeclaringExecutable().getAnnotation(ParameterizedTest.class) != null ? true : false;
}

private TestInstance.Lifecycle determineTestLifecycle(ExtensionContext ec) {
// check the test for org.junit.jupiter.api.TestInstance annotation
TestInstance annotation = ec.getRequiredTestClass().getAnnotation(TestInstance.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jboss.weld.junit5.explicitInjection.parameterizedTest;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

@ApplicationScoped
public class Foo {

@Produces
String s = "fooString";

String ping() {
return Foo.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jboss.weld.junit5.explicitInjection.parameterizedTest;

import java.util.Set;

import jakarta.enterprise.inject.Default;

import org.jboss.weld.junit5.WeldJunit5Extension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@ExtendWith(WeldJunit5Extension.class)
public class ParameterizedTestExplicitInjectionTest {

public static final Set<String> strings = Set.of("one", "two", "three");

@ParameterizedTest
@ValueSource(strings = { "one", "two", "three" })
public void noWeldInjection(String param) {
// String param is not resolved by Weld
Assertions.assertTrue(strings.contains(param));
}

// NOTE: swapping parameters in the below tests leads to a failure! Parameterized test attempts to claim the first
// parameter as its own and cast to given type; there is nothing we can do about that
@ParameterizedTest
@ValueSource(strings = { "one", "two", "three" })
public void parameterizedTestWithWeldInjection(String param, @Default Foo foo) {
// String param is not resolved by Weld
Assertions.assertTrue(strings.contains(param));
// Foo has explicit qualifier and Weld therefore still tried to resolve it
Assertions.assertNotNull(foo);
Assertions.assertEquals(Foo.class.getSimpleName(), foo.ping());
}

@ParameterizedTest
@ValueSource(strings = { "one", "two", "three" })
public void parameterizedTestWithTwoStringParams(String param, @Default String anotherString) {
// String param is not resolved by Weld
Assertions.assertTrue(strings.contains(param));
// Foo has explicit qualifier and Weld therefore still tried to resolve it
Assertions.assertNotNull(anotherString);
Assertions.assertEquals("fooString", anotherString);
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
<version>${version.junit.jupiter}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${version.junit.jupiter}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down

0 comments on commit 12d8a63

Please sign in to comment.