sta*_*ion 0 java equals lombok
我有两个用户定义的对象,其内容相同.但是assertsEquals inturn equals方法为这些对象返回false.
这里是堆栈跟踪,它包含对象的toString内容.
java.lang.AssertionError: expected: com.xyz.test.model.VerificationToken<VerificationToken(id=null, token=, user=User(userId=null, username=null, email=null, password=null, isActive=0, roles=null, imageLocation=null, enabled=false, isAccountNonLocked=true), expiryDate=Sat Dec 22 22:48:49 IST 2018)> but was: com.xyz.test.model.VerificationToken<VerificationToken(id=null, token=, user=User(userId=null, username=null, email=null, password=null, isActive=0, roles=null, imageLocation=null, enabled=false, isAccountNonLocked=true), expiryDate=Sat Dec 22 22:48:49 IST 2018)>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at ....
Run Code Online (Sandbox Code Playgroud)
我对应的课程如下.这有lombak的@data注释,它生成Equals方法.
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class VerificationToken implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8788934708305097680L;
private static final int EXPIRATION = 60 * 24;
@Id
private Long id;
private String token;
private User user;
private Date expiryDate;
public Date calculateExpiryDate(int expiryTimeInMinutes) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MINUTE, expiryTimeInMinutes);
return new Date(cal.getTime().getTime());
}
}
Run Code Online (Sandbox Code Playgroud)
我的用户类如下.这也有@Data Annotation,它生成Equals方法
@Entity
@Table(name = "user")
@Data
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6480423780555564185L;
@Id
private Integer userId;
private String username;
private String email;
@JsonIgnore
private String password;
private int isActive;
private Set<Role> roles;
private String imageLocation;
private boolean enabled;
private boolean isAccountNonLocked;
public User() {
super();
this.enabled=false;
this.isAccountNonLocked= true;
}
public User(User user) {
this.username = user.getUsername();
this.password = user.getPassword();
this.isActive = user.getIsActive();
this.roles = user.getRoles();
this.imageLocation=user.getImageLocation();
this.enabled= user.enabled;
this.isAccountNonLocked = user.isAccountNonLocked;
}
}
Run Code Online (Sandbox Code Playgroud)
我的角色类如下:这也有@Data Annotation,它生成Equals方法
@Entity
@Table(name = "role")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role {
@Id
private Integer roleId;
private String role;
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试用例,其中包含一个对象实例化.
@Test
public void testCreateVerificationToken() {
User user = new User();
String token = new String();
VerificationToken myToken = new VerificationToken();
myToken.setToken(token);
myToken.setUser(user);
myToken.setExpiryDate(myToken.calculateExpiryDate(24*60));
ArgumentCaptor<VerificationToken> entityCaptor = ArgumentCaptor.forClass(VerificationToken.class);
when(mocktokenRepository.save(entityCaptor.capture())).thenReturn(myToken);
verificationTokenServiceImpl.createVerificationToken(user, token);
assertEquals(entityCaptor.getValue(),myToken);
}
Run Code Online (Sandbox Code Playgroud)
另一个对象实例化在此代码中发生.
@Override
public void createVerificationToken(User user, String token) {
VerificationToken myToken = new VerificationToken();
myToken.setToken(token);
myToken.setUser(user);
myToken.setExpiryDate(myToken.calculateExpiryDate(24*60));
tokenRepository.save(myToken);
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么两个对象被认为是不相等的.如果需要,我想提供更多细节.请帮忙.
提前致谢.
您要比较的两个对象在expiryDate字段中具有不同的值.您正在new Date()使用calculateExpiryDate方法为代码中不同点的两个对象生成它们.他们不平等.请注意,日期的精度是毫秒.我不建议您更改calculateExpiryDate要传递的测试的实现,而是可以设置expiryDate为硬编码的Date值.
| 归档时间: |
|
| 查看次数: |
884 次 |
| 最近记录: |