Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not greedily inject @ParameterizedTest; instead treat is as explicit param. injection #170

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading