TNG / junit-dataprovider

A TestNG like dataprovider runner for JUnit with many additional features
Apache License 2.0
246 stars 164 forks source link

Support lists of non-iterable parameterized type #132

Closed aaschmid closed 3 years ago

aaschmid commented 3 years ago

Overview

With the junit4-dataprovider in version 2.7 one cannot use List<UnaryOperator<...>> as the @DataProvider result because of the following exception while trying to calculate test cases:

Dataprovider method '***' must either return Object[][], Object[], String[], Iterable<Iterable<?>>, or Iterable<?>, whereby any subtype of Iterable as well as an arbitrary inner type are also accepted

Reproducer:

    @DataProvider
    public static List<UnaryOperator<String>> listOfUnaryOperator() {
        List<UnaryOperator<String>> list = new ArrayList<>();
        list.add((string) -> "merged" + string);
        return Collections.unmodifiableList(list);
    }

    @Test
    @UseDataProvider("listOfUnaryOperator")
    public void testListOfUnaryOperator(UnaryOperator<String> operator) {
        assertThat(operator.apply("test")).isEqualTo("mergedtest");
    }

Deliverables

Thanks for telling @Airblader :-)

aaschmid commented 3 years ago

Simple but ugly workaround would be using List<List<UnaryOperator<String>>> instead, e.g.:

    @DataProvider
    public static List<List<UnaryOperator<String>>> listOfListOfUnaryOperator() {
        List<List<UnaryOperator<String>>> list = new ArrayList<>();
        List<UnaryOperator<String>> list2 = new ArrayList<>();
        list2.add((string) -> "merged-" + string);
        list.add(list2);
        return Collections.unmodifiableList(list);
    }

    @Test
    @UseDataProvider("listOfListOfUnaryOperator")
    public void testListOfListOfUnaryOperator(UnaryOperator<String> operator) {
        assertThat(operator.apply("test")).isEqualTo("merged-test");
    }