assertj.sersertthat:通过钥匙过滤pojo中的地图条目

发布于 2025-01-26 06:21:21 字数 1306 浏览 3 评论 0原文

我正在尝试做一个主张,以通过其密钥滤除特定的地图条目。唯一可用的忽略方法 - “忽略场”不起作用,因为地图密钥不是字段(显然)。

您知道是否有任何方法可以进行这种过滤?可能是自定义比较器或其他任何东西。

这是问题的代码示例:尝试忽略“第2章”键值比较。

注意:我必须比较的真实对象是自动生成的复杂和深度的,并且可以使用我需要忽略的相同键具有许多不同的地图。

import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class TestAssertJ {

    @Test
    public void testBook() {
        HashMap<String, Chapter> b1Chapters = new HashMap<String, Chapter>() {{
            put("Chapter 1", new Chapter("text 1"));
            put("Chapter 2", new Chapter("text 2"));
        }};
        Book book = new Book("Harry Potter", b1Chapters);

        HashMap<String, Chapter> b2Chapters = new HashMap<String, Chapter>() {{
            put("Chapter 1", new Chapter("text 1"));
        }};
        Book book2 = new Book("Harry Potter", b2Chapters);

        assertThat(book)
                .usingRecursiveComparison()
                .ignoringFields("Chapter 2")
                .isEqualTo(book2);
    }

    @Data
    class Book {
        private final String title;
        private final Map<String, Chapter> chapters;
    }

    @Data
    class Chapter {
        private final String text;
    }
}

I'm trying to do an AssertJ assertion filtering out the specific Map entries by their key. The only available method for ignoring - "ignoringFields" doesn't work since the map key is not a field (obviously).

Do you know if there are any way to do such a filtering? Probably custom comparator or anything else.

Here is the code sample for the problem: trying to ignore "Chapter 2" key value comparison.

Note: The real objects I have to compare are auto-generated complex and deep and can have many different maps with the same keys I need to ignore.

import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class TestAssertJ {

    @Test
    public void testBook() {
        HashMap<String, Chapter> b1Chapters = new HashMap<String, Chapter>() {{
            put("Chapter 1", new Chapter("text 1"));
            put("Chapter 2", new Chapter("text 2"));
        }};
        Book book = new Book("Harry Potter", b1Chapters);

        HashMap<String, Chapter> b2Chapters = new HashMap<String, Chapter>() {{
            put("Chapter 1", new Chapter("text 1"));
        }};
        Book book2 = new Book("Harry Potter", b2Chapters);

        assertThat(book)
                .usingRecursiveComparison()
                .ignoringFields("Chapter 2")
                .isEqualTo(book2);
    }

    @Data
    class Book {
        private final String title;
        private final Map<String, Chapter> chapters;
    }

    @Data
    class Chapter {
        private final String text;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文