首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EqualsVerifier in SpringBoot

EqualsVerifier in SpringBoot
EN

Stack Overflow用户
提问于 2021-03-08 17:32:30
回答 1查看 1.3K关注 0票数 4

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

这是错误

Recursive datastructure. Add prefab values for one of the following types: CustomerView, List<YearConfigView>, YearConfigView

这是@Test类:

代码语言:javascript
复制
@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

代码语言:javascript
复制
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);
    }
}

YearConfigView.java

代码语言:javascript
复制
public class YearConfigView extends EntityBase<Integer> {

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

My problem:,为了解决这个问题,我需要更改或添加什么EqualsVerifier

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-09 07:28:49

这里是EqualsVerifier的创造者。

没有看到您的类(我想确切地看到CustomerViewYearConfigView有哪些字段,以及equalshashCode是如何在这两个类上实现的),很难确定发生了什么,但我怀疑是这样的:

CustomerView有对YearConfigView (或者List<YearConfigView>)的引用,YearConfigView有对CustomerView的引用。

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

避免这种情况的方法是给EqualsVerifier一些“预置值”。我看到您已经尝试过这样做了,添加了.withGenericPrefabValues(CustomerView.class)。(此方法需要2个参数,因此我怀疑您可能在将代码发布到StackOverflow之前删除了一些代码),只有当CustomerView本身是一个泛型类时才能起作用,我无法验证这一点,因为您没有发布特定的代码。无论如何,您不应该为所测试的类提供通用的prefab值或常规的prefab值。

但是,通常情况下,您的测试都应该为另一个类提供一个预置值。看起来是这样的:

代码语言:javascript
复制
@Test
public void CustomerViewTest() {
    YearConfigView one = new YearConfigView();
    one.setYear(2020);
    YearConfigView two = new YearConfigView();
    two.setYear(2021);

    EqualsVerifier.forClass(CustomerView.class)
        .withRedefinedSuperclass()
        .withPrefabValues(YearConfigView.class, one, two)
        .verify();
}

@Test
public void YearConfigViewTest() {
    CustomerView one = new CustomerView();
    one.setName("Alice");
    CustomerView two = new CustomerView();
    two.setName("Bob");

    EqualsVerifier.forClass(YearConfigView.class)
        .suppress(Warning.ALL_FIELDS_SHOULD_BE_USED)
        .withPrefabValues(CustomerView.class, one, two)
        .verify();
}

请注意,我仍然不知道equals方法中包含了哪些字段,因此,我只是对如何实例化类进行了合理的猜测。

有关更多信息,请参见EqualsVerifier文档中的相关页面。由于类是JPA实体,此页也可能有帮助:它解释了@Id是如何被EqualsVerifier对待的。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66534465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档