预期的JUnit断言错误和实际显示的是同一件事 [英] JUnit Assertion error expected and actual showing the same thing

查看:134
本文介绍了预期的JUnit断言错误和实际显示的是同一件事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Assertionerror中的期望值和实际值都显示相同的内容.它们是相同的参考.任何人都知道为什么以及如何解决?下面的代码和错误.预先感谢:

My expected and actual in my Assertionerror are showing the same thing. And they are the same reference. Anyone know why and how to fix? Code and error below. Thanks in advance:

@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]>

推荐答案

这是因为User user = new User("Jerry", "123", "Engineer");正在创建一个新的User对象,而User one = frc.readUser("Jerry");也正在创建一个新的User对象.这两个对象的字段值相同,但是这两个对象不同.但是,您可以通过执行此操作来断言.

That's because User user = new User("Jerry", "123", "Engineer"); is creating a new User object and User one = frc.readUser("Jerry"); is also creating a new User object. these two objects' field values are same but these two objects are different. However you can assert that by doing this.

assertThat(user).isEqualToComparingFieldByField(one);

这篇关于预期的JUnit断言错误和实际显示的是同一件事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆