Java JUnit 5@ParameterizeTest,使用类作为@ValueSource

Java JUnit 5@ParameterizeTest,使用类作为@ValueSource,java,junit5,parameterized,Java,Junit5,Parameterized,我想用@ValueSource进行参数化测试,我已经阅读了很多教程,包括: 命令类: public class Mandator { private String creditorPrefix; private String retailerCode; private int mandateIdSequence; public Mandator(final String creditorPrefix, final String retailerCode, f

我想用
@ValueSource
进行
参数化测试,我已经阅读了很多教程,包括:

命令类:

 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)
  • 枚举类型
  • 注释类型
  • 前面类型的数组(数组数组不是合法的元素类型)

如果您想从创建的对象中提供值,请考虑使用类似的东西作为一个可能的解决方案:

@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));
}
@ParameterizedTest
@方法源(“生成器”)
void testCalculateMandateI(强制值,应为布尔值)
//然后在这个测试班的某个地方
专用静态流生成器(){
回流(
(新委托人(..)的论点,正确),
(新委托人(..),假)的论点;
}

太好了,我只是不明白你在
参数中添加的
true
false
是什么。of()
这应该是一个预期的结果。毕竟给定了不同的参数,您希望验证不同的结果。不过,它不必是布尔值,只需要一些您能够验证的东西。其他人解释了错误的原因,“属性值必须是常量”,您收到。它们都是相关的。
 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;
    }
}
@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));
}