Skip to content

Commit c465613

Browse files
committed
support Iterables and Arrays in the ParameterizedExtension
This also includes support for single parameter tests.
1 parent d282b32 commit c465613

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
3838
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
3939
import org.junit.platform.commons.meta.API;
40+
import org.junit.platform.commons.util.CollectionUtils;
4041
import org.junit.platform.commons.util.ReflectionUtils;
4142
import org.junit.runners.Parameterized;
4243

@@ -243,18 +244,14 @@ private static ParameterResolutionException unMatchedAmountOfParametersException
243244
"The amount of parametersFields in the constructor doesn't match those in the provided parametersFields");
244245
}
245246

246-
private static ParameterResolutionException wrongParametersReturnType() {
247-
return new ParameterResolutionException("The @Parameters returns the wrong type");
248-
}
249-
250247
@SuppressWarnings("unchecked")
251-
private static Collection<Object[]> convertParametersMethodReturnType(Object o) {
252-
if (o instanceof Collection) {
253-
return (Collection<Object[]>) o;
254-
}
255-
else {
256-
throw wrongParametersReturnType();
257-
}
248+
private static Collection<Object[]> convertParametersMethodReturnType(Object obj) {
249+
return CollectionUtils.toStream(obj).map(o -> {
250+
if (o instanceof Object[]) {
251+
return (Object[]) o;
252+
}
253+
return new Object[] { o };
254+
}).collect(toList());
258255
}
259256

260257
private static class ParameterWrapper {

junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void parametrizedWithParameterFieldInjection() {
4949
}
5050

5151
@ExtendWith(ParameterizedExtension.class)
52-
protected static class FibonacciTest {
52+
private static class FibonacciTest {
5353
@Parameters
5454
public static Collection<Object[]> data() {
5555
return Arrays.asList(
@@ -88,7 +88,7 @@ void paremeterizedWithConstructorInjection() {
8888
}
8989

9090
@ExtendWith(ParameterizedExtension.class)
91-
protected static class ParameterizedTestWithConstructor {
91+
private static class ParameterizedTestWithConstructor {
9292
@Parameters
9393
public static Collection<Object[]> data() {
9494
return Arrays.asList(
@@ -116,7 +116,7 @@ void unMatchedConstructorArgumentCount() {
116116
}
117117

118118
@ExtendWith(ParameterizedExtension.class)
119-
protected static class UnMatchedConstructor {
119+
private static class UnMatchedConstructor {
120120
@Parameters
121121
public static Collection<Object[]> data() {
122122
return Arrays.asList(new Object[][] { { 0, 2 } });
@@ -140,7 +140,7 @@ void unMatchedParameterFieldsCount() {
140140
}
141141

142142
@ExtendWith(ParameterizedExtension.class)
143-
protected static class WrongParameters {
143+
private static class WrongParameters {
144144
@Parameterized.Parameter
145145
public int a;
146146
@Parameterized.Parameter(1)
@@ -168,7 +168,7 @@ void noInjectionMix() {
168168
}
169169

170170
@ExtendWith(ParameterizedExtension.class)
171-
protected static class DoubleInjection {
171+
private static class DoubleInjection {
172172
@Parameterized.Parameter
173173
public int a;
174174

@@ -220,7 +220,7 @@ void emptyParametersList() {
220220
}
221221

222222
@ExtendWith(ParameterizedExtension.class)
223-
protected static class EmptyParameters {
223+
private static class EmptyParameters {
224224

225225
public EmptyParameters() {
226226
}
@@ -243,7 +243,7 @@ void duplicatedParameterFieldIndex() {
243243
}
244244

245245
@ExtendWith(ParameterizedExtension.class)
246-
protected static class DuplicatedIndex {
246+
private static class DuplicatedIndex {
247247
@Parameterized.Parameter
248248
public int a;
249249

@@ -268,7 +268,7 @@ void parametersAreOnlyCalledOnce() {
268268
}
269269

270270
@ExtendWith(ParameterizedExtension.class)
271-
protected static class ParametersCalledOnce {
271+
private static class ParametersCalledOnce {
272272
private static int invocationCount = 0;
273273

274274
public ParametersCalledOnce(int a) {
@@ -294,7 +294,7 @@ void multipleTestTemplatesShouldBeRun() {
294294
}
295295

296296
@ExtendWith(ParameterizedExtension.class)
297-
static class MultipleTestTemplates {
297+
private static class MultipleTestTemplates {
298298
private static int invocationCount = 0;
299299

300300
public MultipleTestTemplates(int a) {
@@ -319,6 +319,30 @@ void secondTemplate() {
319319
}
320320
}
321321

322+
@Test
323+
void singleParameterIterable() {
324+
ExecutionEventRecorder eventRecorder = executeTestsForClass(IterableTest.class);
325+
assertThat(eventRecorder.getTestSuccessfulCount()).isEqualTo(2);
326+
}
327+
328+
@ExtendWith(ParameterizedExtension.class)
329+
private static class IterableTest {
330+
331+
public IterableTest(Object o) {
332+
333+
}
334+
335+
@Parameters
336+
public static Object[] data() {
337+
return new Object[] { new Object(), new Object() };
338+
}
339+
340+
@TestTemplate
341+
void dummy() {
342+
343+
}
344+
}
345+
322346
private ExecutionEventRecorder executeTestsForClass(Class<?> testClass) {
323347
LauncherDiscoveryRequest request = request().selectors(selectClass(testClass)).build();
324348
JupiterTestEngine engine = new JupiterTestEngine();

0 commit comments

Comments
 (0)