当顶级值是地图时,AssertJ usingRecursiveComparison 失败

问题描述

我想使用RecursiveComparison将POJO与带有Asserj的地图进行比较

@Data
@AllArgsConstructor
public class Person {
   private String name;
}

@Test
void test(){
    assertthat(new Person("Foo"))
        .usingRecursiveComparison()
        .isEqualTo(Map.of("name","Foo"));
}

这是控制台日志:

java.lang.AssertionError: 
Expecting:
Person(name=Foo)
to be equal to:
{"name"="Foo"}
when recursively comparing field by field,but found the following difference:

Top level actual and expected objects differ:
- actual value   : Person(name=Foo)
- expected value : {"name"="Foo"}
expected field is a map but actual field is not

但这个已弃用的断言有效:

@Test
void test(){
    assertthat(new Person("Foo"))
      .isEqualToComparingFieldByField(Map.of("name","Foo"));
}

这也有效:

@Data
@AllArgsConstructor
public class PersonDto {
    private String name;
}

@Test
void test(){
    assertthat(new Person("Foo"))
        .usingRecursiveComparison()
        .isEqualTo(new PersonDto("Foo"));
}

如何修复第一个

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)