Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equals is not fully tested when extending objects #67

Open
Santobert opened this issue Sep 17, 2020 · 4 comments
Open

Equals is not fully tested when extending objects #67

Santobert opened this issue Sep 17, 2020 · 4 comments
Milestone

Comments

@Santobert
Copy link

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 Mixermachine added this to the version 1.1 milestone Oct 30, 2020
@Mixermachine
Copy link
Owner

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
Copy link
Owner

Mixermachine commented Nov 14, 2020

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
Copy link
Author

Santobert commented Nov 15, 2020

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
Copy link
Owner

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

I will look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants