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);}}