GOX*_*LUS 9 java parameterized junit5
我想做一个ParameterizedTest,@ValueSource我已经阅读了很多教程,包括JUnit 5 参数化测试指南:
/**
* The {@link Class} values to use as sources of arguments; must not be empty.
*
* @since 5.1
*/
Class<?>[] classes() default {};
Run Code Online (Sandbox Code Playgroud)
所以我尝试了以下方法:
@ParameterizedTest
@ValueSource(classes = {new Mandator("0052", "123456", 79)})
void testCalculateMandateI(Mandator value) {
//code using Mandator here
}
Run Code Online (Sandbox Code Playgroud)
委托人类别:
public class Mandator {
private String creditorPrefix;
private String retailerCode;
private int mandateIdSequence;
public Mandator(final String creditorPrefix, final String retailerCode, final int mandateIdSequence) {
this.creditorPrefix = creditorPrefix;
this.retailerCode = retailerCode;
this.mandateIdSequence = mandateIdSequence;
}
public String getCreditorPrefix() {
return creditorPrefix;
}
public String getRetailerCode() {
return retailerCode;
}
public int getMandateIdSequence() {
return mandateIdSequence;
}
}
Run Code Online (Sandbox Code Playgroud)
但我从 IntelliJ 收到以下错误,悬停在 @ValueSource 上方:
属性值必须是常量
我在这里做错了什么?我缺少什么?
Mar*_*nik 23
它不是关于 JUnit 而是关于 Java 语法。
不可能在注释的参数中创建新对象。
注释元素的类型是以下之一:
如果您想从创建的对象中提供值,请考虑使用类似这样的方法作为一种可能的解决方案:
@ParameterizedTest
@MethodSource("generator")
void testCalculateMandateI(Mandator value, boolean expected)
// and then somewhere in this test class
private static Stream<Arguments> generator() {
return Stream.of(
Arguments.of(new Mandator(..), true),
Arguments.of(new Mandator(..), false));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18479 次 |
| 最近记录: |