在 JUnit 中使用参数化 class 时出现 Varargs 错误

Varargs error when using Parameterized class in JUnit

我正在使用 Parameterized class 创建一个测试用例以提高可伸缩性。我有类似以下内容...

@RunWith(Parameterized.class)
public class Test
{
    private final translator;

    @Parameterized.Parameters(name = "translate {0}")
    public static Collection parameters()
    {
        return Arrays.asList(new Object[][] {
                { Translate.EN },
                { Translate.FR }
        });
    }

    public Test1(Translate translate)
    {
        this.translator = new TranslatorImpl(translate);
    ...
    ...

我有一个 Linter 为以下部分抛出错误...

return Arrays.asList(new Object[][] {
        { Translate.EN },
        { Translate.FR }
});

它说...

There's no point in creating an array solely for the purpose of passing it as a varargs (...) argument; varargs is an array. Simply pass the elements directly. They will be consolidated into an array automatically. Incidentally passing an array where Object ... is expected makes the intent ambiguous: Is the array supposed to be one object or a collection of objects?

我可能需要添加更多参数,因此使用 Parameterized class 很有帮助,我想使用它,但我不确定如何解决我的问题我得到了,如果它甚至值得解决。任何想法都会有所帮助。

Arrays.asList 的签名是 public static <T> List<T> asList(T... a) 因此,您可以将单独的数组元素提供给它,而不是将数组提供给 Arrays.asList。在这种特殊情况下:

return Arrays.asList(
  new Translate[] { Translate.EN },
  new Translate[] { Translate.FR }
);