sin*_*d95 5 spring-boot equalsverifier
tests我正在为共享数据列表的两个类创建。当我使用时EqualsVerifier,我收到一个错误,因为它要求我提供包含这两个类共享的数据的列表。
这是错误:
Recursive datastructure. Add prefab values for one of the following types: CustomerView, List<YearConfigView>, 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();
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
YearConfigView.java:
public class YearConfigView extends EntityBase<Integer> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private CustomerView customer;
private Integer year;
private String comments;
}
Run Code Online (Sandbox Code Playgroud)
我的问题:我必须更改或添加什么EqualsVerifier才能解决问题?
EqualsVerifier 的创建者在这里。
如果没有看到您的类(我想确切地了解CustomerView和YearConfigView具有哪些字段,以及如何在两个类上实现equals和hashCode),很难确定发生了什么,但我怀疑是这样的:
CustomerView引用了YearConfigView(或者可能List<YearConfigView>),并且YearConfigView引用了CustomerView。
EqualsVerifier 在做它的事情时,尝试创建它正在验证的类的实例,并为其字段提供正确的值。为了做到这一点,它必须递归地实例化类的字段并给出这些值。通常,这不是问题,但有时您会遇到循环,就像您的情况一样:为了创建 的值CustomerView,它必须具有 的值YearConfigView,反之亦然。
避免这种情况的方法是为 EqualsVerifier 提供一些“预制值”。我发现您已经尝试通过添加.withGenericPrefabValues(CustomerView.class). (此方法需要 2 个参数,因此我怀疑您可能在将其发布到 StackOverflow 之前删除了一些代码)这仅在CustomerView本身是一个泛型类时才有效,我无法验证它,因为您没有发布该特定代码段。无论如何,您都不应该为您正在测试的类提供通用预制件值或常规预制件值。
不过,一般来说,您的测试都应该为另一个类提供预制值。那看起来像这样:
@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();
}
Run Code Online (Sandbox Code Playgroud)
请注意,我仍然不知道您的equals方法中包含哪些字段,因此我只是对如何实例化您的类进行有根据的猜测。
有关更多信息,请参阅EqualsVerifier 文档中的相关页面。由于类是 JPA 实体,因此此页面也可能会有所帮助:它解释了@IdEqualsVerifier 如何处理类。
| 归档时间: |
|
| 查看次数: |
2336 次 |
| 最近记录: |