Open Santobert opened 4 years ago
Hy @Santobert,
yes this seems like a blind spot. The fix might take some time as I'm currently loaded with my thesis.
Thanks for bringing it up :)
Hy @Santobert
I have worked on this issue for some time now. The line
if (getClass() != obj.getClass())
return false;
is not the problem.
The problem is generating a class that fullfills the combination of
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
A class which has the same super class, but not the Child class itself. So for example: Parent class: Mammal Child class: Human
I would need to create an extending class (an Elephant class for example) on the fly. It would be possible to search for other classes with the same super class, but this will fail if there is no other class that extends the super class (Mammal). -> Flaky tests
Java Reflections can not define classes on the fly (at least not to my knowledge). Thats where custom classloader and byte code manipulation come into play. Something like https://stackoverflow.com/a/2320465 (requires sun dependencies, which are not included in the OpenJDK) or the PowerMock lib (https://github.com/powermock/powermock) can help there. The jOOR lib (https://github.com/jOOQ/jOOR) seems to work well, although there will be problems with classloader.
I guess I can get it to work, but this will definitely add a lot of execution time to the tests and I doubt that I can get it to work over all suported Java version (8 - 13) seamlessly.
Maybe I don't see the easy solution. What are your thoughts @Santobert ?
What do you think about giving the user the option to create this class. That works great for test data providers. Why not for this case as well?
Yes that is definitely an option. Something like addSibling(Class<?> siblingClass)
I will look into it.
Situation
There are two classes that extend each other. The methods
hashCode()
andequals()
were both generated by Eclipse.This is the base test for the class Child:
Issue
The following line in the Childs function
equals()
is not tested:I guess it needs an object that is an instance of
Parent
but not aChild
to test this case.