Junit 5 - 如何为@CsvSource 传入多个空值?

Junit 5 - How to pass in multiple null values for @CsvSource?

我正在将测试方法从单个参数修改为多个:

@ParameterizedTest
@NullSource
@ValueSource({"foo", "bar"..})
void shouldReturnFalse(String x) {
  assertThat(someMethod(x)).isFalse();
}

@ParameterizedTest
@CsvSource({
  "null, null",
  "foo, bar"
})
void shouldReturnFalse(String x, String y) {
  assertThat(someMethod(x, y)).isFalse();
}

null 这里作为字符串文字而不是空文字传入。结果,这个测试失败了。此测试以前使用 @NullSource 的单个参数,但切换到多个时会出现以下错误:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter...

我找不到解决此问题的方法,而且我看到的解决方案相当笨拙和繁琐。有没有更简单的方法来提供 null 的值?

@CsvSource 有一个名为 nullValues 的属性。 见 documentation.

A list of strings that should be interpreted as null references.

@CsvSource(value= {"null, null",
                   "foo, bar"}
           , nullValues={"null"})

另一种选择是不传递先前链接文档中所述的任何值。

Please note that unquoted empty values will always be converted to null references regardless of the value of this nullValues attribute; whereas, a quoted empty string will be treated as an emptyValue().

@CsvSource({",",
            "foo, bar"})

您可以使用上面由@Jan Schmitz 解释的 nullValues,或者您可以像这样丢弃空值:

@CsvSource({
  ",",
  "foo, bar"
})