I have an unexpected and confusing issue with the instance comparing.
So, by order:
I try to compare two instance of the same class and override the hashcode()
and equals()
methods:
public class AdminUser {
.............
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj == null){
return false;
}
Class c1 = obj.getClass(); //-----------{1}
Class c2 = this.getClass(); //----------{2}
if (this.getClass() != obj.getClass()){
return false;
}
AdminUser other = (AdminUser)obj;
return Objects.equals(this.username, other.username) &&
Objects.equals(this.username, other.username) &&
Objects.equals(this.username, other.username);
}
}
- The method, from where I compare:
public class AdminCRUD extends BaseWithPerformanceTest {
private void assertIfUserExist(AdminUser cau) {
AdminUser createdUser = createUserForm.getUserFromForm(); //----{3}
if(!cau.equals(createdUser)){
saveScreenshot("Users_dont_match");
}
}
}
- The instance where I take
AdminUser
({3}):
public class UsersPage extends TopBarConsoleMenu {
..................
public class CreateUserForm extends BaseForm {
public AdminUser getUserFromForm() {
......................
return new AdminUser(){{
setEmail(ACTUAL_EMAIL);
setName(ACTUAL_NAME);
setUsername(ACTUAL_USER_NAME);
setUserType(EnumUtils.getEnumByMethodName(AdminUserType.class, "getAddUserDropDownValue", ACTUAL_USER_TYPE));
setRegion(EnumUtils.getEnumByMethodName(AdminRegion.class, "getDropdownValue", ACTUAL_REGION));
}};
}
}
}
So the issue:
(please see AdminUser
class)
- In the point {1},
c1
variable got thepages.admin.UsersPage$CreateUserForm$1
instance! - In the point {2}
c2
variable as expectedutils.data.admin.AdminUser$1
Except other - the obj
instance, actually contains correct instance! - After boxing AdminUser other = (AdminUser)obj;
I can get any correct data from the other
variable - there are really AdminUser
instance!
What are happens?