From ca6df6c6401c2f5d01109fed27e1d75c01c78a6c Mon Sep 17 00:00:00 2001 From: Liam Cark Date: Mon, 10 Apr 2017 17:08:03 +0200 Subject: [PATCH] support Iterables and Arrays in the ParameterizedExtension This also includes support for single parameter tests. --- .../parameterized/ParameterizedExtension.java | 15 +++---- .../ParameterizedExtensionTests.java | 42 +++++++++++++++---- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java b/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java index b09adbc83a0a..f6becdf85ed7 100644 --- a/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java +++ b/junit-jupiter-migration-support/src/main/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtension.java @@ -37,6 +37,7 @@ import org.junit.jupiter.api.extension.TestTemplateInvocationContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; import org.junit.platform.commons.meta.API; +import org.junit.platform.commons.util.CollectionUtils; import org.junit.platform.commons.util.ReflectionUtils; import org.junit.runners.Parameterized; @@ -248,13 +249,13 @@ private static ParameterResolutionException wrongParametersReturnType() { } @SuppressWarnings("unchecked") - private static Collection convertParametersMethodReturnType(Object o) { - if (o instanceof Collection) { - return (Collection) o; - } - else { - throw wrongParametersReturnType(); - } + private static Collection convertParametersMethodReturnType(Object obj) { + return CollectionUtils.toStream(obj).map(o -> { + if (o instanceof Object[]) { + return (Object[]) o; + } + return new Object[] { o }; + }).collect(toList()); } private static class ParameterWrapper { diff --git a/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java b/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java index 9eb0a40ca03a..5c49dd1ce492 100644 --- a/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java +++ b/junit-jupiter-migration-support/src/test/java/org/junit/jupiter/migrationsupport/parameterized/ParameterizedExtensionTests.java @@ -49,7 +49,7 @@ void parametrizedWithParameterFieldInjection() { } @ExtendWith(ParameterizedExtension.class) - protected static class FibonacciTest { + private static class FibonacciTest { @Parameters public static Collection data() { return Arrays.asList( @@ -88,7 +88,7 @@ void paremeterizedWithConstructorInjection() { } @ExtendWith(ParameterizedExtension.class) - protected static class ParameterizedTestWithConstructor { + private static class ParameterizedTestWithConstructor { @Parameters public static Collection data() { return Arrays.asList( @@ -116,7 +116,7 @@ void unMatchedConstructorArgumentCount() { } @ExtendWith(ParameterizedExtension.class) - protected static class UnMatchedConstructor { + private static class UnMatchedConstructor { @Parameters public static Collection data() { return Arrays.asList(new Object[][] { { 0, 2 } }); @@ -140,7 +140,7 @@ void unMatchedParameterFieldsCount() { } @ExtendWith(ParameterizedExtension.class) - protected static class WrongParameters { + private static class WrongParameters { @Parameterized.Parameter public int a; @Parameterized.Parameter(1) @@ -168,7 +168,7 @@ void noInjectionMix() { } @ExtendWith(ParameterizedExtension.class) - protected static class DoubleInjection { + private static class DoubleInjection { @Parameterized.Parameter public int a; @@ -220,7 +220,7 @@ void emptyParametersList() { } @ExtendWith(ParameterizedExtension.class) - protected static class EmptyParameters { + private static class EmptyParameters { public EmptyParameters() { } @@ -243,7 +243,7 @@ void duplicatedParameterFieldIndex() { } @ExtendWith(ParameterizedExtension.class) - protected static class DuplicatedIndex { + private static class DuplicatedIndex { @Parameterized.Parameter public int a; @@ -268,7 +268,7 @@ void parametersAreOnlyCalledOnce() { } @ExtendWith(ParameterizedExtension.class) - protected static class ParametersCalledOnce { + private static class ParametersCalledOnce { private static int invocationCount = 0; public ParametersCalledOnce(int a) { @@ -294,7 +294,7 @@ void multipleTestTemplatesShouldBeRun() { } @ExtendWith(ParameterizedExtension.class) - static class MultipleTestTemplates { + private static class MultipleTestTemplates { private static int invocationCount = 0; public MultipleTestTemplates(int a) { @@ -319,6 +319,30 @@ void secondTemplate() { } } + @Test + void singleParameterIterable() { + ExecutionEventRecorder eventRecorder = executeTestsForClass(IterableTest.class); + assertThat(eventRecorder.getTestSuccessfulCount()).isEqualTo(2); + } + + @ExtendWith(ParameterizedExtension.class) + private static class IterableTest { + + public IterableTest(Object o) { + + } + + @Parameters + public static Object[] data() { + return new Object[] { new Object(), new Object() }; + } + + @TestTemplate + void dummy() { + + } + } + private ExecutionEventRecorder executeTestsForClass(Class testClass) { LauncherDiscoveryRequest request = request().selectors(selectClass(testClass)).build(); JupiterTestEngine engine = new JupiterTestEngine();