我想对ParameterizedTest
做@ValueSource
,我已经阅读了包括Guide to JUnit 5 Parameterized Tests在内的许多教程:
/**
* The {@link Class} values to use as sources of arguments; must not be empty.
*
* @since 5.1
*/
Class<?>[] classes() default {};
所以我尝试了以下方法:
@ParameterizedTest
@ValueSource(classes = {new Mandator("0052", "123456", 79)})
void testCalculateMandateI(Mandator value) {
//code using Mandator here
}
强制类:
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;
}
}
但是我从IntelliJ中收到以下错误,将鼠标悬停在@ValueSource上方:
属性值必须为常数
我在这里做错了什么?我想念什么?
答案 0 :(得分:0)
这与JUnit无关,而与Java语法有关。
不可能在注释的参数中创建新对象。
注释元素的类型为以下之一:
如果要从已创建的对象中提供值,请考虑使用以下方法作为一种可能的解决方案:
const data = [{
"code": "200",
"msg": "success",
"data": {
"list": [{
"name": "A",
"age": "10",
"logo": "aa.png",
"note": "aa"
}, {
"name": "B",
"age": "20",
"logo": "bb.png",
"note": "bb"
}],
"show": [
"name",
"age"
]
}
},
{
"code": "200",
"msg": "success",
"data": {
"list": [{
"name": "C",
"age": "30",
"logo": "aa.png",
"note": "aa"
}, {
"name": "D",
"age": "40",
"logo": "bb.png",
"note": "bb"
}],
"show": [
"name",
"age"
]
}
}];
function init() {
return data.reduce((acc, res) => {
if (res.code == 200) {
return [...acc, ...res.data.list.map((obj) => {
return res.data.show.reduce((acc, prop) => ({...acc, [prop]: obj[prop]}), {});
})];
}
return acc;
}, []);
}
console.log(init());