简体   繁体   English

Junit 4 assertEquals(...)无法比较两个对象

[英]Junit 4 assertEquals(…) fails to compare two objects

Error message: 错误信息:

Expected: UserDTO<UserDTO{username='user', password='password', email='mail@mail'}> 
Actual: UserDTO<UserDTO{username='user', password='password', email='mail@mail'}>

The only difference is a whitespace after the '>' of the expected stringified object. 唯一的区别是期望的字符串化对象的'>'之后的空格。 I do not know why, because i compare two UserDTO objects. 我不知道为什么,因为我比较了两个UserDTO对象。 Has anyone had the same issue? 有人遇到过同样的问题吗?

UPDATE: The equals implementation of UserDTO. 更新:等于UserDTO的实现。

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserDTO userDTO = (UserDTO) o;

        if (username != null ? !username.equals(userDTO.username) : userDTO.username != null) return false;
        if (password != null ? !password.equals(userDTO.password) : userDTO.password != null) return false;
        return  (email != null ? !email.equals(userDTO.email) : userDTO.email != null);

    }

This 这个

return  (email != null ? !email.equals(userDTO.email) : userDTO.email != null);

returns false if the emails are equal, and true if they are unequal. 如果电子邮件相等,则返回false;如果电子邮件不相等,则返回true。

You mean something like this: 您的意思是这样的:

return (email==null ? userDTO.email==null : email.equals(userDTO.email));

The comparison failure message gives you a state of objects what you expect and what you get. 比较失败消息为您提供了对象的状态,它们是您期望和得到的。
The fact that the two objects show the same state but that these are not equals for assertEquals() means that the equals() method doesn't rely on the value of the declared fields or it may also means that equals() contains logic error(s) . 这两个对象显示相同的状态,但对于assertEquals()这些对象不等于事实,这意味着equals()方法不依赖于声明的字段的值,或者也可能意味着equals()包含逻辑错误(s)。

And for you it is the second case as this : 对您来说,这是第二种情况:

 return  (email != null ? !email.equals(userDTO.email) : userDTO.email != null);

should be : 应该 :

return  (email != null ? email.equals(userDTO.email) : userDTO.email == null);

as if email is not null , you want to return the equals() result on this field while if email is null , you want to return true if other.email == null and false otherwise. 就像email不为null ,您想在此字段上返回equals()结果,而如果emailnullother.email == nullother.email == null return true ,否则return false

Anyway, the equals() method could be much simplified by using Objects.equals() to compare each field as it spares you from doing a null check for each field : 无论如何,可以通过使用Objects.equals()比较每个字段来简化equals()方法,因为这样Objects.equals()对每个字段进行null检查:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    UserDTO userDTO = (UserDTO) o;
    return (Objects.equal(username, userDTO.username) && Objects.equal(password, userDTO.password) && Objects.equal(email, userDTO.email));        
}     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM