Skip to content

Commit 9105afe

Browse files
committed
Allow ArgumentsAggregator implementations to use constructor injection
Issue: #4018
1 parent 125e10b commit 9105afe

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestMethodContext.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.junit.jupiter.params.converter.DefaultArgumentConverter;
3333
import org.junit.jupiter.params.support.AnnotationConsumerInitializer;
3434
import org.junit.platform.commons.support.AnnotationSupport;
35-
import org.junit.platform.commons.support.ReflectionSupport;
3635
import org.junit.platform.commons.util.StringUtils;
3736

3837
/**
@@ -200,7 +199,7 @@ Resolver createResolver(ParameterContext parameterContext, ExtensionContext exte
200199
try { // @formatter:off
201200
return AnnotationSupport.findAnnotation(parameterContext.getParameter(), AggregateWith.class)
202201
.map(AggregateWith::value)
203-
.map(clazz -> (ArgumentsAggregator) ReflectionSupport.newInstance(clazz))
202+
.map(clazz -> ParameterizedTestSpiInstantiator.instantiate(ArgumentsAggregator.class, clazz, extensionContext))
204203
.map(Aggregator::new)
205204
.orElse(Aggregator.DEFAULT);
206205
} // @formatter:on

junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/ArgumentsAggregator.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.apiguardian.api.API;
1616
import org.junit.jupiter.api.extension.ParameterContext;
17+
import org.junit.jupiter.api.extension.ParameterResolver;
1718

1819
/**
1920
* {@code ArgumentsAggregator} is an abstraction for the aggregation of arguments
@@ -33,10 +34,11 @@
3334
* in a CSV file into a domain object such as a {@code Person}, {@code Address},
3435
* {@code Order}, etc.
3536
*
36-
* <p>Implementations must provide a no-args constructor and should not make any
37-
* assumptions regarding when they are instantiated or how often they are called.
38-
* Since instances may potentially be cached and called from different threads,
39-
* they should be thread-safe and designed to be used as singletons.
37+
* <p>Implementations must provide a no-args constructor or a single unambiguous
38+
* constructor to use {@linkplain ParameterResolver parameter resolution}. They
39+
* should not make any assumptions regarding when they are instantiated or how
40+
* often they are called. Since instances may potentially be cached and called
41+
* from different threads, they should be thread-safe.
4042
*
4143
* @since 5.2
4244
* @see AggregateWith

jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,13 @@ void injectsParametersIntoArgumentConverterConstructor() {
12251225
.testEvents() //
12261226
.assertStatistics(it -> it.succeeded(1));
12271227
}
1228+
1229+
@Test
1230+
void injectsParametersIntoArgumentsAggregatorConstructor() {
1231+
execute(SpiParameterInjectionTestCase.class, "argumentsAggregatorWithConstructorParameter", String.class) //
1232+
.testEvents() //
1233+
.assertStatistics(it -> it.succeeded(1));
1234+
}
12281235
}
12291236

12301237
// -------------------------------------------------------------------------
@@ -2176,6 +2183,13 @@ void argumentConverterWithConstructorParameter(
21762183
assertEquals("resolved value", argument);
21772184
}
21782185

2186+
@ParameterizedTest
2187+
@ValueSource(strings = "value")
2188+
void argumentsAggregatorWithConstructorParameter(
2189+
@AggregateWith(ArgumentsAggregatorWithConstructorParameter.class) String argument) {
2190+
assertEquals("resolved value", argument);
2191+
}
2192+
21792193
record ArgumentsProviderWithConstructorParameter(String value) implements ArgumentsProvider {
21802194

21812195
@Override
@@ -2191,6 +2205,15 @@ public Object convert(Object source, ParameterContext context) throws ArgumentCo
21912205
return value;
21922206
}
21932207
}
2208+
2209+
record ArgumentsAggregatorWithConstructorParameter(String value) implements ArgumentsAggregator {
2210+
2211+
@Override
2212+
public Object aggregateArguments(ArgumentsAccessor accessor, ParameterContext context)
2213+
throws ArgumentsAggregationException {
2214+
return value;
2215+
}
2216+
}
21942217
}
21952218

21962219
private static class TwoSingleStringArgumentsProvider implements ArgumentsProvider {

0 commit comments

Comments
 (0)