assertEquals(Object o1,Object o2)是否使用equals方法 [英] Does assertEquals(Object o1, Object o2) uses the equals method

查看:116
本文介绍了assertEquals(Object o1,Object o2)是否使用equals方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说,assertEquals是否适用于覆盖等于

解决方案

来自 assertEquals方法的源代码您可以在 Junit GitHub Repo 上找到:

  / ** 
*断言两个对象相等。如果它们不是
*,则会在给定消息的情况下抛出AssertionFailedError。
* /
static public void assertEquals(String message,Object expected,Object actual){
if(expected == null&& actual == null){
return ;
}
if(expected!= null&& expected.equals(actual)){
return;
}
failNotEquals(消息,预期,实际);
}

你可以看到Junit正在使用 .equals ()方法。



修改:



代码段来自不推荐使用的Junit版本。



您可以阅读新Junit的来源此处。这个想法几乎相同,也使用 .equals()方法。


In other words, does assertEquals works with a class that overrides equals

解决方案

From the source code of the assertEquals method that you can find on the Junit GitHub Repo:

/**
 * Asserts that two objects are equal. If they are not
 * an AssertionFailedError is thrown with the given message.
 */
static public void assertEquals(String message, Object expected, Object actual) {
    if (expected == null && actual == null) {
        return;
    }
    if (expected != null && expected.equals(actual)) {
        return;
    }
    failNotEquals(message, expected, actual);
}

You can see that Junit is using the .equals() method.

Edit:

The code snippet is coming from a deprecated version of Junit.

You can read about the source of the 'new' Junit here. The idea is pretty much the same, the .equals() method is also used.

这篇关于assertEquals(Object o1,Object o2)是否使用equals方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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