我正在尝试使用5.11版本中引入的
@MethodSources
。
Javadoc
class MapFieldsTest {
static Stream<Arguments> testGetIntCommon() {
return Stream.of(
Arguments.of("a", 10),
Arguments.of("b", 20)
);
}
static Stream<Arguments> testGetIntBroken() {
return Stream.of(
Arguments.of("c", null),
Arguments.of("d", null)
);
}
@ParameterizedTest
@MethodSources({
@MethodSource("testGetIntCommon"),
@MethodSource("testGetIntBroken")
})
void testGetInt(String givenKey, Integer expected) {
// THIS DOESN'T WORK
}
@ParameterizedTest
@MethodSource("testGetIntCommon")
void testGetIntCommon(String givenKey, Integer expected) {
// THIS WORKS
}
@ParameterizedTest
@MethodSource("testGetIntBroken")
void testGetIntBroken(String givenKey, Integer expected) {
// THIS WORKS
}
}
但是我收到错误:
MapFieldsTest.testGetInt(String, Integer) » PreconditionViolation Configuration error: You must configure at least one set of arguments for this @ParameterizedTest
@MethodSources
设置可能存在什么问题?
从 JUnit 5.11 开始,
@MethodSource
是可重复的注释,@MethodSources
用作支持容器。看来 JUnit 的注释处理不检查 @MethodSources
,而仅检查其嵌套元素。试试这个:
@ParameterizedTest
@MethodSource("testGetIntCommon"),
@MethodSource("testGetIntBroken")
void testGetInt(String givenKey, Integer expected) {
...
}