是否可以比较两种不同类型的对象与莫克托(Mockito)或hamcrest进行比较?例如,下面我显示了我的对象的示例。
objecta:
public class ObjectA {
private Long id;
private String name;
private AddressA address
// getters and setters
}
objectb:
public class ObjectB {
private Long id;
private String name;
private AddressB address
// getters and setters
}
对象adversa和addressb也具有相同的命名属性,但只有不同的类型。有可能吗?谢谢。
使用hamcrest
API,您可以通过遵循
ObjectA A = new ObjectA(1, "ABC", new AddressA("jkl"));
ObjectB B = new ObjectB(1, "PQR", new AddressB("xyz"));
assertThat(A, hasProperty("id", equalTo(B.getId())));
assertThat(A, hasProperty("name", equalTo(B.getName())));
assertThat(A.getAddressA(), hasProperty("address", equalTo(B.getAddressB().getAddress())));