How to parameterize with String arrays in JUnit 5

后端 未结 3 2084
不思量自难忘°
不思量自难忘° 2021-02-07 14:17

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



        
3条回答
  •  滥情空心
    2021-02-07 14:52

    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 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.

提交回复
热议问题