JUnit 5 @ParameterizedTest 使用 class 作为 @ValueSource
JUnit 5 @ParameterizedTest with using class as @ValueSource
我想用 @ValueSource
做一个 ParameterizedTest
,我已经阅读了很多教程,包括 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
}
委托人class:
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 上方:
属性值必须是常量
我在这里做错了什么?我错过了什么?
它不是关于 JUnit,而是关于 Java 语法。
无法在注解参数中新建对象
注释元素的类型是以下之一:
- 列表原始类型(int、short、long、byte、char、double、float 或 boolean)
- 字符串
- Class(带有可选的类型参数,例如 Class)
- 枚举类型
- 注释类型
- 上述类型的数组(数组的数组不是合法的元素类型)
如果您想从创建的对象中提供值,请考虑使用类似这样的方法作为一种可能的解决方案:
@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));
}
我想用 @ValueSource
做一个 ParameterizedTest
,我已经阅读了很多教程,包括 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
}
委托人class:
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 上方:
属性值必须是常量
我在这里做错了什么?我错过了什么?
它不是关于 JUnit,而是关于 Java 语法。
无法在注解参数中新建对象
注释元素的类型是以下之一:
- 列表原始类型(int、short、long、byte、char、double、float 或 boolean)
- 字符串
- Class(带有可选的类型参数,例如 Class)
- 枚举类型
- 注释类型
- 上述类型的数组(数组的数组不是合法的元素类型)
如果您想从创建的对象中提供值,请考虑使用类似这样的方法作为一种可能的解决方案:
@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));
}