java - Apache BeanUtils behavioral conflict while copying List items from different classes -
i developing application copy data of 1 class class.
i trying test getter-setter method works fine or not. improve test coverage making class , methods use podamfactory fill data inside given class's members.
then copying data of object other object using apache beanutils.
then asserting both objects check both have same data or not !
the problem facing that: have create 3 classes:
- starter.java [have main method run application]
- childclass.java [have 1 member i.e. collection of string , it's getter method , add method add data inside member]
- parentclass.java [inside class childclass's instance have been created , instantiated , related getter , setter method. have hashcode , 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 passing parentclass check it's getter setter. testclasses method main work of copy , assertion.
when debugging found after copying properties of class, child class's instance have list , have 5 members. in both object there's 5 members in list element.
but when passing childclass in main try debug getting 5 element in source , in destination class have 0 element after copying properties.
so don't understand behavior of beanutils, need if want same work when passing childclass in main ?
when passing parentclass it's have different behavior , when passing childclass have different behavior in copying properties.
i have created github repository show code. can access , check code of application.
i have created simple gradle project dependency management.
if knows more let me know.
thanks.
beanutils follows strict definition of properties in java bean sense, i.e. each property must have getter name get + name of field , setter name set + name of field. while child class contains no setfields() setter. in other words change this
public class childclass { private final list<string> fields = new arraylist<>(); public list<string> getfields() { return this.fields; } public list<string> setfields(list<string> fields) { this.fields = fields; } } or alternatively
public class childclass { private final list<string> fields = new arraylist<>(); public list<string> getfields() { return this.fields; } public list<string> setfields(list<string> fields) { this.fields.clear(); this.fields.addall(fields); } }
Comments
Post a Comment