Testing BeanUtils / test should fail when creating a new property

I use BeanUtils to map some DTO class to Domain classes (and vice versa / versa). (using BeanUtils copy properties)

I want to check my code. How to write a test that fails if someone writes creates an additional property in the DTO or domain class.

My attempt, which I'm still working on, is to go through the BeanUtils.getPropertyDescriptors (class) and find the corresponding getter THEN methods for each class (DTO and Domain) to check for equality.

Any thoughts?

Due to the limitations of the project dependency, I would prefer not to use something like Dozer. I am using spring 3 beanutils.

+4
source share
1 answer

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.

+1
source

All Articles