samedi 21 mars 2015

Unit Test in Java of a Guava Function used to trasform two objects with same fields

I have a very specific use case, that occurs every time you have two objects A and B that have the same non-static fields and you want to transform one in the other. I personally use Function from Guava, as follows:



public class A{
private static serialVersionUID = ....

private int field1;
private String field2;
}

public class B{
private int field1;
private String field2;

public B(final int field1, final String field2){
this.field1 = field1;
this.field2 = field2;
}
}

public class AToBTransformer implements Function<A, B> {

@Nullable
@Override
public B apply(final A aObj) {
return new B(a.field1, a.field2);
}

}


Now, in the unit test I can check if the AToBTransformer is going to return an instance of B with the same values of the instance of A in all the common non-static fields. I mean, they should provide the same mapping in all the future version of the code, so if one field is added in A then it should be provided also in B.


Is there any reasonable way to check that automatically in the unit test?


I was thinking about counting the number of fields, or using reflection to campare them, but I'm not sure that this is the best approach.


Aucun commentaire:

Enregistrer un commentaire