How to properly unit test disposable objects?
I am wondering what is the best way to unit test Android parcelable objects. I know how to test test objects using Parcel.obtain()
and write it to a plot and create it using the creator. The problem with this method is that it won't catch if someone adds a field to the object without adding it to the boolean / equals / hashcode logic. Is there a library or method that exists to confirm that all object fields are written to the parcel? I know not all fields need to be separated, I submit an annotation or something in the field to determine if it should be included in the unit test.
source to share
You can use EqualsBuilder.reflectionEquals from Apache Commons Lang
In build.gradle
:
testCompile 'org.apache.commons:commons-lang3:3.4'
Then you can specify which fields to ignore or to ignore transient fields
User one = new User();
one.setName("Salem")
one.setLastName("Saberhagen");
one.setAddress("Somewhere");
one.setAge(1000);
User two = one.clone();
two.setAddress("Mars");
two.setAge(2000);
reflectionEquals(one, two); // -> False
reflectionEquals(one, two, "address", "age"); // -> True
source to share