My goal is to test the equals
and hashCode
methods of all our domain classes (located under x.x.x.domain.models) without repeating code. Therefore, I had decided to use the equals verifier library. This library takes as input the Class
of the class to be tested.
Since I want this test to be fully automated and without repeated code I was using, in combination with the previous library, the Reflections Library as in this answer, in order to fetch every Class
located in the domain package.
There is a little catch, though. Since the test is in the same package as the rest of the domain classes, I have to avoid it, which seamed simple enough. However, much to my surprise the following code:
private boolean isSameClass(Class c) {
System.out.println("this class: " + this.getClass());
System.out.println("reflection: " + c);
System.out.println("-----" + c.equals(this.getClass()));
return c.equals(this.getClass());
}
Used like this:
Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class);
for (Class c : classes) {
if(!this.isSameClass(c)) {
/* ^^^^^^ */
EqualsVerifier.forClass(c).usingGetClass().verify();
}
}
yields:
this class: class com.lastminute.cobos.domain.model.EqualityTest
reflection: class com.lastminute.cobos.domain.model.CityTest
----- false
This needs a bit of explanation. CityTest
was the old name of the test, now it is called EqualityTest
. However, it seems that something has not notice this change. I am curious about how is this behavior possible.
Update
I was able to make the test work doing a clean
before running the test. However, I am still curious about why this.getClass()
returned the right Class
while the reflection library found ye olde one.
Aucun commentaire:
Enregistrer un commentaire