How to parameterize with String arrays in JUnit 5

泄露秘密 提交于 2019-12-05 22:18:47

问题


I would like to write JUnit 5 parametrized test which takes string array (String[]) as a parameter:

@ParameterizedTest
@MethodSource("stringArrayProvider")
void parseFirstAndSecondInt(String[] args) {
    Arguments arguments = new Arguments(args);

    assertEquals(1, arguments.getFirst());
    assertEquals(2, arguments.getSecond());
}

I'm not sure, how to provide a collection/stream/iterator of string arrays. I've unsuccessfully tried following approach with @MethodSource annotation

static Stream<String[]> stringArrayProvider() {
    return Stream.of(
            new String[]{"1", "2"},
            new String[]{"1", "2", "3"});
}

but I'm receiving this exception:

org.junit.jupiter.params.converter.ArgumentConversionException:
    No implicit conversion to convert object of type java.lang.String to type [Ljava.lang.String;

What would be a good design/solution to have such kind of parameterized test?


回答1:


Use the Arguments.of() factory from org.junit.jupiter.params.provider.Arguments to wrap your arguments:

static Stream<Arguments> stringArrayProvider() {
    return Stream.of(
            Arguments.of((Object) new String[]{"1", "2"}),
            Arguments.of((Object) new String[]{"1", "2", "3"})
    );
}

For details see http://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests




回答2:


First one general rule of thumb that I use:

  • use @ArgumentSource (your solution) when the same generated test cases can be used by more than one Test Class

  • use @MethodSource (Sormuras solution) when the same generated test cases can be used by more than one Test Method (in the same class)

  • otherwise try to keep the source for test cases as local as possible to the method that uses them

In this last situation I envision two simple possibilities:

  1. you are interested in a fixed number of strings (so not really need for an array). You can use @CsvSource

    Here there is an example for two strings (possibly including commas too).

    @ParameterizedTest
    @CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" })
    void testWithCsvSource(String first, String second) {
        assertNotNull(first);
        assertNotEquals(0, second);
    }
    

    Note that in this case, if the various elements are not really strings they can be probably automatically parsed to the correct types (e.g., in your question it seems that you want integers)

  2. you want really a variable size array of Strings. In this case you can use @ValueSource and then convert its content to String[]

    directly:

    @ParameterizedTest
    @ValueSource(strings = {"1, 2",
                            "1, 2, 3"})
    void testWithArrayOfStrings(String arg) {       // the single csv String parameter
      String[] arrayParam = arg.split("\\s*,\\s*"); // is converted to an array of Strings
      ...
    }
    

    or with an Explicit Converter class indicated by @ConvertWith :

    @ParameterizedTest
    @ValueSource(strings={"1, 2", "1, 2, 3"})
    void testWithArrayOfStrings(@ConvertWith(CSVtoArray.class)String... arg) 
    {
      ...
    }
    
    public static class CSVtoArray extends SimpleArgumentConverter {
      @Override
      protected Object convert(Object source, Class<?> targetType) throws ArgumentConversionException {
        String s = (String) source;
        return s.split("\\s*,\\s*");
      }
    }
    



回答3:


An alternative for Sormuras' solution can be usage of the annotation @ArgumentsSource which works in very similar manner:

static class StringArrayProvider implements ArgumentsProvider {
    @Override
    public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
        return Stream.of(
                (Object) new String[]{"1", "2"},
                (Object) new String[]{"1", "2", "3"}).map(Arguments::of);
    }
}

Still, casting String[] to Object looks strange and I have rather feeling of workaround than of nice design.



来源:https://stackoverflow.com/questions/46931267/how-to-parameterize-with-string-arrays-in-junit-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!