I am developing an application that copy data of one class to another class.
I am trying to test getter-setter method works fine or not. To improve test coverage I am making some class and methods that will use podamfactory to fill up data inside given class's members.
Then I am copying data of that object to other object using apache BeanUtils.
Then I am asserting both objects to check that both have same data or not !
The problem I am facing that: I have create 3 classes:
- Starter.java [have main method to run application]
- ChildClass.java [have only 1 member i.e. Collection of String and it's getter method and add method to add data inside that member]
- ParentClass.java [inside this class ChildClass's instance have been created and instantiated and related getter and setter method. Also have hashcode and equals method]
Code:
public class Starter {
static final PodamFactory factory = new PodamFactoryImpl();
public static void main(String[] args) throws Exception {
System.out.println("Start");
testClasses(ParentClass.class);
System.out.println("End");
}
public static void testClasses(final Class<?> klass) throws Exception {
final Object source = factory.manufacturePojo(klass);
final Object destination = klass.newInstance();
BeanUtils.copyProperties(destination, source);
Assert.assertEquals(source, destination);
Assert.assertNotEquals(source, new Object());
Assert.assertEquals(source.hashCode(), destination.hashCode());
}
}
In Main method I am passing ParentClass to check it's getter setter. testClasses method do the main work of copy and assertion.
When I am debugging I found that after copying properties of class, child class's instance have list and that have 5 members. In both object there's 5 members in List element.
But When I am passing ChildClass in Main then try to debug I am getting 5 element in source and in destination class it have 0 element after copying properties.
So I don't understand this behavior of BeanUtils, what need to do If I want same work when I am passing ChildClass in Main ?
When I am passing ParentClass it's have different behavior and when I am passing ChildClass it have different behavior in Copying properties.
I have created GitHub Repository to show my code. You can access and check code of my application.
I have created simple gradle project for dependency management.
If anyone knows more about it then let me know.
Thanks.
BeanUtils follows strict definition of properties in Java Bean sense, i.e. each property must have getter with name get + name of a field and setter with name set + name of a field. While your child class contains no setFields() setter. In other words you could change it like this
or alternatively