Mixermachine / base-test

Automated testing of Java classes via reflections
21 stars 3 forks source link

Equals is not fully tested when extending objects #67

Open Santobert opened 4 years ago

Santobert commented 4 years ago

Situation

There are two classes that extend each other. The methods hashCode() and equals() were both generated by Eclipse.

public class Parent {
    int foo;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + foo;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Parent other = (Parent) obj;
        if (foo != other.foo)
            return false;
        return true;
    }
}

public class Child extends Parent {
    int bar;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + bar;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        Child other = (Child) obj;
        if (bar != other.bar)
            return false;
        return true;
    }
}

This is the base test for the class Child:

public class ChildTest {

    @Test
    public void baseTest() {
        SingleThreadExecutor executor = new SingleThreadExecutor();

        assertTrue(executor.execute(Child.class,
                Arrays.asList(new GetterIsSetterCheck(), new HashcodeAndEqualsCheck(), new PublicVariableCheck())));
    }
}

Issue

The following line in the Childs function equals() is not tested:

        if (getClass() != obj.getClass())
            return false;

I guess it needs an object that is an instance of Parent but not a Child to test this case.

Mixermachine commented 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 :)

Mixermachine commented 3 years ago

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 ?

Santobert commented 3 years ago

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?

Mixermachine commented 3 years ago

Yes that is definitely an option. Something like addSibling(Class<?> siblingClass)

I will look into it.