换句话说, assertEquals 是否与覆盖的类一起使用equals
7 回答
您可以在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()
也使用了方法。
assertEquals 是否适用于覆盖等于的类?
是的,如果类有一个,则assertEquals()
调用重写。equals()
是的,它调用equals
并且有一个单独的方法,assertSame
它使用==
. 只是为了澄清问题,因为所有对象都声明了,所以assertEquals
可以与任何equals
对象一起使用。
是的,它确实。
Object m = new Object() {
@Override
public boolean equals(Object obj) {
System.out.println("proof!");
return super.equals(obj);
}
};
Assert.assertEquals(m, m);
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
.
是 - 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);
}
一般来说,答案是“是”,但是除非文档明确指定断言方法的行为,否则不应假设equals
在所有情况下都会调用该断言方法。例如,何时expected == actual
或actual == null
断言方法可能在根本不调用的情况下快速返回equals
。
JUnit 5's Assertions
class mentions this explicitly:
Assertion methods comparing two objects for equality, such as the
assertEquals(expected, actual)
andassertNotEquals(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 implementsObject.equals(Object)
. For example,assertEquals()
might immediately returntrue
when provided the same object for the expected and actual values, without callingequals(Object)
at all. Tests that aim to verify theequals(Object)
implementation should instead be written to explicitly verify theObject.equals(Object)
contract by usingassertTrue()
orassertFalse()
— for example,assertTrue(expected.equals(actual))
,assertTrue(actual.equals(expected))
,assertFalse(expected.equals(null))
, etc.