You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
However for a simple pojo listed below pojo-tester (v 0.7.6) throws:
Class (...)MyPojoTest.MyPojo has bad 'hashCode' method implementation.
The hashCode method should return different hash codes for non equal objects.
The test code:
package com.my.pojo;
import org.junit.jupiter.api.Test;
import java.util.Objects;
import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor;
// hashCode() and equals() generated with IntelliJ
class MyPojoTest {
public static class MyPojo {
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MyPojo myPojo = (MyPojo) o;
return age == myPojo.age &&
Objects.equals(name, myPojo.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
@Test
public void testMyPojo() {
assertPojoMethodsFor(MyPojo.class).areWellImplemented();
}
}
The text was updated successfully, but these errors were encountered:
This error is thrown when we try to use any type of collection object as a member variable inside our POJO class.
This issue is still not fixed in this repository and there is a forked project being updated - https://github.com/obsidiandynamics/pojo-tester also has the same issue which makes this hashcode testing feature of the project unusable currently.
Since this project is now on hold, I hope the maintainer gets good collaborative support & this project to be resumed soon along with fixes for this issue.
The code given below is a valid POJO. Java POJOs are allowed to return same hashCode() for non-equal objects. Might not be optimal, but it's valid and actually fairly common situation - see https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
However for a simple pojo listed below pojo-tester (v 0.7.6) throws:
The test code:
The text was updated successfully, but these errors were encountered: