我有使用 TestNG 的单元测试,我尝试迁移到 JUnit Jupiter (JUnit 5),我想知道哪种方法最好:
测试NG:
@DataProvider
public Object[][] invalidPortNumbers() {
return new Object[][] {
{ "--http", "" },
{ "--http", "-42" },
{ "--http", "0" },
{ "--http", "not_a_port_number" },
{ "--https", "67000" }
};
}
@Test(dataProvider = "invalidPortNumbers",
expectedExceptions = ParameterException.class,
expectedExceptionsMessageRegExp = ".* is not valid port number .*")
public void shouldFailToValidatePortNumber(final String... args) {
new CommandLineParser(args);
}
我看到转移到JUnit Jupiter,我可以这样做:
static Stream<Arguments> invalidPortNumbers2() {
return Stream.of(
Arguments.of((Object) new String[] { "--http", "-42" }),
Arguments.of((Object) new String[] { "--http", "0" }),
Arguments.of((Object) new String[] { "--http", "not_a_port_number" }),
Arguments.of((Object) new String[] { "--https", "67000" })
);
}
@ParameterizedTest
@MethodSource("invalidPortNumbers2")
void shouldFailToValidatePortNumber(final String... args) {
assertThatThrownBy(() -> new CommandLineParser(args))
.isInstanceOf(ParameterException.class)
.hasMessageMatching(".* is not valid port number .*");
}
有没有其他方法可以简化这一点并保留以前的 dataProvider 结构以最小化更改?
谢谢。