57

换句话说, assertEquals 是否与覆盖的类一起使用equals

4

7 回答 7

52

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

/**
 * 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);
}

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

编辑:

代码片段来自已弃用的 Junit 版本。

您可以在此处阅读有关“新”Junit 的来源。思路大同小异,.equals()也使用了方法。

于 2012-11-28T14:42:43.447 回答
7

assertEquals 是否适用于覆盖等于的类?

是的,如果类有一个,则assertEquals()调用重写。equals()

于 2012-11-28T14:41:34.637 回答
4

是的,它调用equals并且有一个单独的方法,assertSame它使用==. 只是为了澄清问题,因为所有对象都声明了,所以assertEquals可以与任何equals对象一起使用。

于 2012-11-28T14:42:23.200 回答
2

是的,它确实。

Object m = new Object() {

    @Override
    public boolean equals(Object obj) {
        System.out.println("proof!");
        return super.equals(obj);
    }
};
Assert.assertEquals(m, m);
于 2012-11-28T14:41:49.303 回答
1

JUnit 是开源的,因此您可以自己在源代码中查找:https ://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/Assert.java

static public void assertEquals(String message, Object expected,
        Object actual) {
    if (equalsRegardingNull(expected, actual)) {
        return;
    } else if (expected instanceof String && actual instanceof String) {
        String cleanMessage = message == null ? "" : message;
        throw new ComparisonFailure(cleanMessage, (String) expected,
                (String) actual);
    } else {
        failNotEquals(message, expected, actual);
    }
}

private static boolean equalsRegardingNull(Object expected, Object actual) {
    if (expected == null) {
        return actual == null;
    }

    return isEquals(expected, actual);
}

private static boolean isEquals(Object expected, Object actual) {
    return expected.equals(actual);
}

所以你可以看到它使用equals. 如果要比较身份,请使用assertSame.

于 2012-11-28T14:44:59.433 回答
1

是 - TestNG 覆盖等于 -

static public void assertEquals(Object actual, Object expected, String message) {
    if((expected == null) && (actual == null)) {
      return;
    }
    if(expected != null) {
      if (expected.getClass().isArray()) {
        assertArrayEquals(actual, expected, message);
        return;
      } else if (expected.equals(actual)) {
        return;
      }
    }
    failNotEquals(actual, expected, message);
  }
于 2014-11-10T09:55:41.580 回答
0

一般来说,答案是“是”,但是除非文档明确指定断言方法的行为,否则不应假设equals在所有情况下都会调用该断言方法。例如,何时expected == actualactual == null断言方法可能在根本不调用的情况下快速返回equals

JUnit 5's Assertions class mentions this explicitly:

Assertion methods comparing two objects for equality, such as the assertEquals(expected, actual) and assertNotEquals(unexpected, actual) variants, are only intended to test equality for an (un-)expected value and an actual value. They are not designed for testing whether a class correctly implements Object.equals(Object). For example, assertEquals() might immediately return true when provided the same object for the expected and actual values, without calling equals(Object) at all. Tests that aim to verify the equals(Object) implementation should instead be written to explicitly verify the Object.equals(Object) contract by using assertTrue() or assertFalse() — for example, assertTrue(expected.equals(actual)), assertTrue(actual.equals(expected)), assertFalse(expected.equals(null)), etc.

于 2021-10-25T17:12:00.843 回答