In other words, does assertEquals works with a class that overrides equals
换句话说,assertEquals是否与覆盖equals的类一起使用
6 个解决方案
#1
35
From the source code of the assertEquals method that you can find on the Junit GitHub Repo:
从您可以在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);
}
You can see that Junit is using the .equals()
method.
你可以看到Junit正在使用.equals()方法。
Edit:
The code snippet is coming from a deprecated version of Junit.
代码段来自不推荐使用的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.
你可以在这里阅读关于'新'Junit的来源。这个想法几乎相同,也使用.equals()方法。
#2
7
does assertEquals works with a class that overrides equals?
assertEquals是否与覆盖等于的类一起使用?
Yes, assertEquals()
invokes the overridden equals()
if the class has one.
是的,如果类有一个,则assertEquals()调用重写的equals()。
#3
4
Yes, it calls equals
and there is a separate method, assertSame
, that uses ==
. Just to clear things up, assertEquals
works with any object since all objects declare equals
.
是的,它调用equals,并且有一个单独的方法assertSame,它使用==。只是为了清理,assertEquals适用于任何对象,因为所有对象都声明为equals。
#4
2
Yes it does.
是的,它确实。
Object m = new Object() {
@Override
public boolean equals(Object obj) {
System.out.println("proof!");
return super.equals(obj);
}
};
Assert.assertEquals(m, m);
#5
1
yes - TestNG overrides equals -
是的 - 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);
}
#6
1
JUnit is opensource, so you could have looked it up yourself in the source: https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/Assert.java
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);
}
So as you can see it uses equals
. If you want to compare identity, you use assertSame
.
所以你可以看到它使用equals。如果要比较标识,请使用assertSame。
#1
35
From the source code of the assertEquals method that you can find on the Junit GitHub Repo:
从您可以在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);
}
You can see that Junit is using the .equals()
method.
你可以看到Junit正在使用.equals()方法。
Edit:
The code snippet is coming from a deprecated version of Junit.
代码段来自不推荐使用的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.
你可以在这里阅读关于'新'Junit的来源。这个想法几乎相同,也使用.equals()方法。
#2
7
does assertEquals works with a class that overrides equals?
assertEquals是否与覆盖等于的类一起使用?
Yes, assertEquals()
invokes the overridden equals()
if the class has one.
是的,如果类有一个,则assertEquals()调用重写的equals()。
#3
4
Yes, it calls equals
and there is a separate method, assertSame
, that uses ==
. Just to clear things up, assertEquals
works with any object since all objects declare equals
.
是的,它调用equals,并且有一个单独的方法assertSame,它使用==。只是为了清理,assertEquals适用于任何对象,因为所有对象都声明为equals。
#4
2
Yes it does.
是的,它确实。
Object m = new Object() {
@Override
public boolean equals(Object obj) {
System.out.println("proof!");
return super.equals(obj);
}
};
Assert.assertEquals(m, m);
#5
1
yes - TestNG overrides equals -
是的 - 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);
}
#6
1
JUnit is opensource, so you could have looked it up yourself in the source: https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/Assert.java
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);
}
So as you can see it uses equals
. If you want to compare identity, you use assertSame
.
所以你可以看到它使用equals。如果要比较标识,请使用assertSame。