簡體   English   中英

SpringBoot 中的 EqualsVerifier

[英]EqualsVerifier in SpringBoot

我正在為共享數據列表的兩個類創建tests 當我使用EqualsVerifier時出現錯誤,因為它要求我提供一個包含這兩個類共享的數據的列表。

這是錯誤

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

這是@Test class:

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

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

我的問題:我必須在EqualsVerifier中更改或添加什么來解決問題?

EqualsVerifier 的創建者在這里。

沒有看到你的類(我想看看CustomerViewYearConfigView有哪些字段,以及如何在兩個類上實現equalshashCode ),很難確定發生了什么,但我懷疑是這樣的:

CustomerView有對YearConfigView的引用(或者可能是List<YearConfigView> ),並且YearConfigView有對CustomerView的引用。

EqualsVerifier 在做它的事情時,會嘗試創建它正在驗證的類的實例,並給它的字段提供適當的值。 為了做到這一點,它必須遞歸地實例化類的字段並給出這些值。 通常,這不是問題,但有時你會遇到一個循環,就像你的情況一樣:為了為CustomerView創建一個值,它必須有一個YearConfigView的值,反之亦然。

避免這種情況的方法是給 EqualsVerifier 一些“預制值”。 我看到您已經嘗試通過添加.withGenericPrefabValues(CustomerView.class)來做這樣的事情。 (此方法需要 2 個參數,因此我懷疑您可能在將其發布到 StackOverflow 之前刪除了一些代碼)這僅適用於CustomerView本身是通用 class 的情況,我無法驗證,因為您沒有發布那段特定的代碼。 無論如何,您不應該為您正在測試的 class 提供通用預制值或常規預制值。

不過,總的來說,您的測試都應該為另一個 class 提供預制值。 看起來像這樣:

@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 實體,因此此頁面也可能會有所幫助:它解釋了 EqualsVerifier 如何處理@Id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM