如何断言两个 Class 类型的 ArrayList 其中包含 class 类型的元素?

How to assert two Class type ArrayList which contains class type elements?

我有两个列表,例如 List<A> listExpectedList<A> listActual。我想比较这些列表的元素,但问题是这些列表的某些元素属于 Class 类型,所以我的断言 AssertEqual 失败了。知道我如何断言这两个列表而不用手动逐一断言每个元素,因为有超过 30 个 elements.Thanks。 下面是我得到的列表元素的示例

Example

List<A> listExpected =[elem1, elem2, Obj@33322, elem3];
List<A> listActual =[elem1, elem2, Obj@346762, elem3];

Dummy Code

Class CashWire {
    String name = "Name";
    String add = "Address";
    String a;
    String b;
    DeliveryInfo diInfo = new DeliveryInfo(name, add);
    public CashWire(String a, String b, diInfo) {
        this.a = a;
        this.b = b;
        this.diInfo = diInfo;
    }
}

class DeliveryInfo {
    String name;
    String add;
    public DeliveryInfo(String name, String add) {
        this.name = name;
        this.add = add;
    }
}

class Main {
    public m1() {
        List <CashWire> list = (List<CashWire>) obj.getData() //will return object type data                                                                
    //    list =<[CashWire[a = "Some String", b = "Some String", 
    //                        diInfo = DeliveryInfo @7872348]]> 
    }
}

实际上我找到了一个适合我的解决方案

Mockito 提供 reflection-matcher:

For latest version of Mockito use:

Assert.assertTrue(new ReflectionEquals(expected, excludeFields).matches(actual));

For older versions use:

Assert.assertThat(actual, new ReflectionEquals(expected, excludeFields));