Skip to content

Commit

Permalink
support Iterables and Arrays in the ParameterizedExtension
Browse files Browse the repository at this point in the history
This also includes support for single parameter tests.
  • Loading branch information
LiamClark committed Apr 10, 2017
1 parent d282b32 commit ca6df6c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -248,13 +249,13 @@ private static ParameterResolutionException wrongParametersReturnType() {
}

@SuppressWarnings("unchecked")
private static Collection<Object[]> convertParametersMethodReturnType(Object o) {
if (o instanceof Collection) {
return (Collection<Object[]>) o;
}
else {
throw wrongParametersReturnType();
}
private static Collection<Object[]> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void parametrizedWithParameterFieldInjection() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class FibonacciTest {
private static class FibonacciTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
Expand Down Expand Up @@ -88,7 +88,7 @@ void paremeterizedWithConstructorInjection() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class ParameterizedTestWithConstructor {
private static class ParameterizedTestWithConstructor {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
Expand Down Expand Up @@ -116,7 +116,7 @@ void unMatchedConstructorArgumentCount() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class UnMatchedConstructor {
private static class UnMatchedConstructor {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 2 } });
Expand All @@ -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)
Expand Down Expand Up @@ -168,7 +168,7 @@ void noInjectionMix() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class DoubleInjection {
private static class DoubleInjection {
@Parameterized.Parameter
public int a;

Expand Down Expand Up @@ -220,7 +220,7 @@ void emptyParametersList() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class EmptyParameters {
private static class EmptyParameters {

public EmptyParameters() {
}
Expand All @@ -243,7 +243,7 @@ void duplicatedParameterFieldIndex() {
}

@ExtendWith(ParameterizedExtension.class)
protected static class DuplicatedIndex {
private static class DuplicatedIndex {
@Parameterized.Parameter
public int a;

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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();
Expand Down

0 comments on commit ca6df6c

Please sign in to comment.