【发布时间】:2019-06-12 16:08:58
【问题描述】:
我的 Assertionerror 中的预期和实际显示相同的内容。它们是相同的参考。有谁知道为什么以及如何解决?下面的代码和错误。提前致谢:
@Test
public void test_CreateAUserWritesAFileReadsFilePrintsFile() throws IOException {
//Arrange
WriteCommand fwc = new FileWriteCommand();
ReadCommand frc = new FileReadCommand();
RegistrationController rc = new RegistrationController();
User user = new User("Jerry", "123", "Engineer");
rc.registerNewUser("Jerry", "123", "Engineer");
fwc.writeUser(user);
User one = frc.readUser("Jerry");
System.out.println(one);
User expected = one;
//Act
User actual = user;
//Assert
assertEquals(expected, actual);
}
错误
java.lang.AssertionError: expected: com.fdmgroup.userregistration.User<User [username=Jerry, password=123, role=Engineer]> but was: com.fdmgroup.userregistration.User<User [username=Jerry, password=123, role=Engineer]>
【问题讨论】:
-
可能您在用户类中的 equals() 实现对相等性的理解与您不同..
-
用户'user'和用户'one'是不同的对象。尝试打印用户对象。
-
换句话说:阅读 minimal reproducible example 并通过添加有关 User 类的详细信息来相应地增强您的问题。
-
谢谢!非常感谢