BeanUtilsBeans copyProperties not copying any fields

1.4k Views Asked by At

I am trying to use Apache Commons BeanUtils to copy fields from a source object to a destination object. However, when testing my code, the properties are not copied at all. Following is the code I am using

public class CopyUtilTest {

    public void copySourceToDestination(Object destinationObject, Object sourceObject) throws ApiException {
        BeanUtilsBean beanUtil = new BeanUtilsBean();
        try {
            beanUtil.copyProperties(destinationObject, sourceObject);
        } catch (InvocationTargetException | IllegalAccessException e) {
            throw new ApiException("Error in copying properties using [NullAwarePropertyCopy]");
        }
    }

    @Test
    public void testCopy() throws ApiException {
        A source = new A();
        source.setA("John");
        source.setB(23);

        B target = new B();

        copySourceToDestination(target, source);
        System.out.println(target.getA()+" "+target.getB());
    }

}

@Getter
@Setter
class A {
    String a;
    int b;
}

@Getter
@Setter
class B {
    String a;
    int b;
}

The output I get when running this test in Junit is null 0 which clearly indicates that the fields have not been copied. Why could this be happening?

0

There are 0 best solutions below