Skip to content

Equals is not fully tested when extending objects #67

Description

@Santobert

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions