assertj.sersertthat:通过钥匙过滤pojo中的地图条目
我正在尝试做一个主张,以通过其密钥滤除特定的地图条目。唯一可用的忽略方法 - “忽略场”不起作用,因为地图密钥不是字段(显然)。
您知道是否有任何方法可以进行这种过滤?可能是自定义比较器或其他任何东西。
这是问题的代码示例:尝试忽略“第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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论