Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot SpringBoot中的EqualsVerifier_Spring Boot_Equalsverifier - Fatal编程技术网

Spring boot SpringBoot中的EqualsVerifier

Spring boot SpringBoot中的EqualsVerifier,spring-boot,equalsverifier,Spring Boot,Equalsverifier,我正在为共享数据列表的两个类创建测试。当我使用equalsvillier时,我得到一个错误,因为它要求我提供一个包含这两个类共享的数据的列表 这是错误: 递归数据结构。为以下类型之一添加预置值:CustomerView、List、YearConfigView 这是@Test类: @Test public void CustomerViewTest() { EqualsVerifier.forClass(CustomerView.class).withRedefinedSuperclass

我正在为共享数据列表的两个类创建
测试。当我使用
equalsvillier
时,我得到一个错误,因为它要求我提供一个包含这两个类共享的数据的列表

这是错误

递归数据结构。为以下类型之一添加预置值:CustomerView、List、YearConfigView

这是
@Test
类:

@Test
    public void CustomerViewTest() {
EqualsVerifier.forClass(CustomerView.class).withRedefinedSuperclass().withGenericPrefabValues(CustomerView.class).verify();
        }

@Test
    public void YearConfigViewTest() {
        EqualsVerifier.forClass(YearConfigView.class).suppress(Warning.ALL_FIELDS_SHOULD_BE_USED).verify();
    }
CustomerView.java

public class CustomerView extends EntityBase<Integer> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private List<YearConfigView> yearConfigs;

    @JsonProperty("current_year_config")
    public YearConfigView getCurrentYearConfig() {
        if (this.getYearConfigs() == null || this.getYearConfigs().isEmpty()) {
            return null;
        }
        int currentYear = LocalDate.now().getYear();
        return this.yearConfigs.parallelStream().filter(yc -> yc.getYear() == currentYear).findAny().orElse(null);
    }
}
public class YearConfigView extends EntityBase<Integer> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private CustomerView customer;
    private Integer year;
    private String comments;
}

我的问题:要解决问题,我必须更改或添加什么
equalVerifier

equalVerifier的创建者

如果没有看到您的类(我想确切了解
CustomerView
YearConfigView
有哪些字段,以及
equals
hashCode
如何在这两个类上实现),很难说清楚到底发生了什么,但我怀疑是这样的:

CustomerView
引用了
YearConfigView
(或者可能是
List
),而
YearConfigView
引用了
CustomerView

EqualsVerifier在执行其任务时,尝试生成其正在验证的类的实例,并为其字段提供适当的值。为了做到这一点,它必须递归地实例化类的字段并给出这些值。通常,这不是问题,但有时会遇到循环,就像在您的案例中:为了为
CustomerView
创建值,它必须为
YearConfigView
具有值,反之亦然


避免这种情况的方法是给EqualVerifier一些“预置值”。我看到您已经尝试过这样做,通过添加
.WithGenericPrefactValues(CustomerView.class)
。(此方法需要2个参数,因此我怀疑您可能在将其发布到StackOverflow之前删除了一些代码。您是否可以发布
CustomerView
YearConfigView
的代码?我怀疑
CustomerView
YearConfigView
中的一个字段属于其自身类型,它们是
实体
类,
 CustomerView
YearConfigView
收集数据CustomerView和YearConfigView是实体。YearConfigView包含一系列与CustomerView相关的字段。CustomerView从YearConfigView获取数据列表并将其存储在列中。CustomerView取决于YearConfigView中的数据。这是CustomerView方法:
private List yearConfigs;
我用这个数据更新了这个问题。因此,这意味着我的假设是正确的。请注意,EqualVerifier只查看字段,并且
等于
hashCode
方法。它对任何其他方法都不起作用。它是JPA实体的事实也与此无关,除了对于EV不会抱怨非最终问题的事实,pt表示感谢。我已经更新了问题,但我无法解决使用预设值的
问题,如果有什么变化?在
new YearConfigView(…);
new CustomerView(…)中声明什么
?您需要有两个不同的
YearConfigView
实例和两个
CustomerView
实例。我无法查看您的代码库内部;如果您在测试中需要这样一个类的实例,您通常会做什么?这就是我在其他实体中实例化它的方法:
私有CustomerView currentCustomer;@JsonProperty(“current_customer_id”)public void setCurrentCustomerId(整数idCustomer){if(idCustomer==null){this.currentCustomer=null;}else{this.currentCustomer=new CustomerView();this.currentCustomer.setId(idCustomer);}}