base-test icon indicating copy to clipboard operation
base-test copied to clipboard

Equals is not fully tested when extending objects

Open Santobert opened this issue 4 years ago • 4 comments

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.

Santobert avatar Sep 17 '20 19:09 Santobert

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 avatar Oct 30 '20 16:10 Mixermachine

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 ?

Mixermachine avatar Nov 14 '20 23:11 Mixermachine

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?

Santobert avatar Nov 15 '20 20:11 Santobert

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

I will look into it.

Mixermachine avatar Nov 16 '20 17:11 Mixermachine