gpt4 book ai didi

java - AssertEquals 为两个相同的对象返回 false

转载 作者:行者123 更新时间:2023-12-04 12:44:12 46 4
gpt4 key购买 nike

我有两个用户定义的对象,它们的内容相同。但是 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 ....

我对应的类如下。这具有生成 Equals 方法的 lombak 的 @data 注释。

@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());
}

}

我的用户类如下。这也有生成 Equals 方法的 @Data 注解

@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;
}

}

我的角色类如下:这也有@Data Annotation是生成Equals方法

@Entity
@Table(name = "role")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role {

@Id
private Integer roleId;
private String role;

}

这是我的测试用例,其中包含一个对象实例化。

@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);
}

另一个对象实例化发生在这段代码中。

@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);
}

我无法理解为什么两个对象被视为不相等。如果需要,我很乐意提供更多详细信息。请帮忙。

提前致谢。

最佳答案

您正在比较的两个对象在 expiryDate 字段中具有不同的值。您正在使用 calculateExpiryDate 方法为代码中不同点的两个对象使用 new Date() 生成它们。他们不会平等。请注意,日期的精度是毫秒。我不会建议您更改 calculateExpiryDate 实现以使测试通过,相反您可以将 expiryDate 设置为硬编码的日期值。

关于java - AssertEquals 为两个相同的对象返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888879/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com