If you are only concerned with testing additional properties, your testing method might look like this:
void assertSameProperties(Class class1, Class class2) { Set<String> properties = new HashSet<String>(); for (PropertyDescriptor prop : BeanUtils.getPropertyDescriptors(class1)) { properties.add(prop.getName()); } for (PropertyDescriptor prop : BeanUtils.getPropertyDescriptors(class2)) { if (!properties.remove(prop.getName()) { fail("Class " + class2.getName() + " has extra property " + prop.getName()); } } if (!properties.isEmpty()) { fail("Class " + class1.getName() + " has extra properties"); } }
If you are interested in testing the comparison itself, then your approach with calling getters for each property that exists in both classes and checking their results for equality should work. Remember the "class" property, however, its value will certainly be different for objects of different classes.
source share