I have some objects (of different class) say PersonType1 and PersonType2 and trying to compare the fields are equal in the test case. The scenario is like:
jsonA -> PersonTypeA;
jsonB -> PersonTypeB;
both PersonTypeA.class and PersonTypeB.class have the same properties, say id, name, etc.
I am trying to assert the values are equal like this:
assertEquals(personA.getId(), personB.getId());
I am not able to use standard reflection equals provided by Mockito as the classes are not same. I am not planning a write a bunch of extra code to compare the obejcts. Something more in the line of:
Assert.assertTrue(new ReflectionEquals(expected, excludeFields).matches(actual)); //mockito
to
Assert.assertTrue(compareProperties(expected, excludeFields).matches(actual));
Edit 1: This is not a duplicate question I am trying to compare 2 different objects of different classes if they have the same value in properties which have the same name. I can write the code but looking for some existing util methods if already present in junit5, mockito, hamcrest, etc
assertEquals(objA.getId(), objB.getId());
//like this there are 30 properties
also there are nested objects like list, set of Strings. Comparing them manually is too much pain
Edit 2: Maybe this will explain better
I do not have control on the POJOs. They are owned by someone else. So, essentially if I have 2 classes
class Cat{
String id;
String name;
// 30 more properties
}
class Dog{
String id;
String name;
// 30 more properties
}
How to compare cat and dog have same values in id, name, etc. Because there are so many properties I do not want to write
assertEquals(cat.getId(), dog.getId());
So, is there any utility to do that? We can do the other way round, if we have 2 objects (of different classes) we can copy the properties using BeanUtils.copyProperies(o1, o2)
in Spring and apache bean utils. Similarly is there a way to compare the properties?