Java 为什么junit 中的assertEquals 和assertSame 为同一类的两个实例返回相同的结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28451925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why assertEquals and assertSame in junit return the same result for two instances same class?
提问by rawData
According to documentation
根据文档
assertEquals() Asserts that two objects are equal.
assertSame() Asserts that two objects refer to the same object.
assertEquals() 断言两个对象相等。
assertSame() 断言两个对象引用同一个对象。
So I am expecting that if I have a class like below
所以我期待如果我有一个像下面这样的课程
class SomeClass {}
then
然后
SomeClass someClass1= new SomeClass();
SomeClass someClass2= new SomeClass();
assertSame(someClass1,someClass2); // fail
assertEquals(someClass1,someClass2); // fail
the assertEquals should pass and assertSame should fail, as the value of both classes are equal but they have different reference location.
assertEquals 应该通过而 assertSame 应该失败,因为两个类的值相等但它们具有不同的引用位置。
As I get failure in both cases then my question is what are the difference between these two ?
当我在这两种情况下都失败时,我的问题是这两者之间有什么区别?
采纳答案by Alexis C.
Since you didn't override equals in your class, assertEquals
behaves the same as assertSame
since the default equals implementation compare references.
由于您没有在类中覆盖 equals,因此assertEquals
行为与assertSame
默认 equals 实现比较引用相同。
150 public boolean equals(Object obj) {
151 return (this == obj);
152 }
If you provide a dumb overriding of equals:
如果你提供了一个愚蠢的 equals 覆盖:
class SomeClass {
@Override
public boolean equals(Object o) {
return true;
}
}
you'll see that assertEquals
succeeds.
你会看到它assertEquals
成功了。
回答by mkrakhin
assertEquals
uses equals()
method (that you should override in your class to really compare its instances) to compare objects, while assertSame
uses ==
operator to compare them. So the difference is exactly the same as between ==
(compare by value) and equals
(compare identity).
assertEquals
使用equals()
方法(您应该在类中重写以真正比较其实例)来比较对象,而assertSame
使用==
运算符来比较它们。所以区别与==
(compare by value) 和equals
(compare identity)之间的区别完全相同。
回答by Boris Pavlovi?
The first assert fails because someClass1
and sameClass2
are not the same instances. The second assert fails because equals(Object)
method hasn't been defined in SomeClass
and its super equals(Object)
does reference equality. Since two different instances are compared for equality, this one fails for the same reason as the first.
第一个断言失败,因为someClass1
和sameClass2
不是相同的实例。第二个断言失败,因为equals(Object)
方法尚未定义,SomeClass
并且其 superequals(Object)
确实引用了相等性。由于比较两个不同的实例是否相等,这个失败的原因与第一个相同。
回答by KeyMaker00
Official JUnit documentation:
官方 JUnit 文档:
assertEquals: Asserts that two objects are equal.
assertSame: Asserts that two objects refer to the same object.
assertEquals:断言两个对象相等。
assertSame:断言两个对象引用同一个对象。
In other words
换句话说
assertEquals: uses the equals() method, or if no equals() method was overridden, compares the reference between the 2 objects.
assertSame: compares the reference between the 2 objects.
assertEquals:使用 equals() 方法,或者如果没有覆盖 equals() 方法,则比较 2 个对象之间的引用。
assertSame:比较两个对象之间的引用。
Example 1: equals method was notoverridden, so assertSame and assertEquals return the same result, since they compare the objects' reference.
示例 1:equals 方法未被覆盖,因此 assertSame 和 assertEquals 返回相同的结果,因为它们比较对象的引用。
public class A {
private int i;
public A(int i){ this.i = i; }
}
public class TestA {
final A a1 = new A(0);
final A a2 = new A(0);
@Test
public void assertsame_testAssertSame(){
assertSame(a1, a2); // AssertionError: expected:<test2.A@7f13d6e> but was:<test2.A@51cdd8a>
}
@Test
public void assertsame_testAssertEquals(){
assertEquals(a1, a2); // AssertionError: expected:<test2.A@7f13d6e> but was:<test2.A@51cdd8a>
}
}
Example 2: equals method was overridden, so assertSame and assertEquals return the not same result, since the equals method will be used by assertEquals this time.
示例 2:equals 方法被覆盖,因此 assertSame 和 assertEquals 返回不同的结果,因为这次 assertEquals 将使用 equals 方法。
public class A {
private int i;
public A(int i){ this.i = i; }
@Override
public boolean equals(Object o){
// self check
if(this == o){ return true; } else
// null check
if(o == null){ return false;} else
// type check and cast
if(getClass() != o.getClass()){ return false; } else {
final A a = (A) o;
// field comparison
return Objects.equals(a, a);
}
}
}
public class TestA {
final A a1 = new A(0);
final A a2 = new A(0);
@Test
public void assertsame_testAssertSame(){
assertSame(a1, a2); // AssertionError: expected:<test2.A@7f13d6e> but was:<test2.A@51cdd8a>
}
@Test
public void assertsame_testAssertEquals(){
assertEquals(a1, a2); // OK
}
}
回答by tfont
assertEquals: ==
assertSame: ===
assertEquals: ==
assertSame: ===
'same' matches the type along with the value which is the same as '==='.
'same' 匹配类型以及与 '===' 相同的值。
回答by Saurabh Oza
assertEquals- It check the objects are equal or not based on the overridded equals() method of that class. So we can check the equality of object based on their state(compare the values of their instance variables).
assertEquals- 它根据该类的重写 equals() 方法检查对象是否相等。所以我们可以根据对象的状态来检查对象的相等性(比较它们的实例变量的值)。
assertEquals() The assertEquals() method compares two objects for equality, using their equals() method.
assertEquals() assertEquals() 方法使用它们的equals() 方法比较两个对象的相等性。
@Test
public void assertEquals_example() {
Employee employeeNew = new Employee();
employee.setSalary(1000000.0);
assertEquals("EMPLOYEE OBJECT", employee, employeeNew);
}
If the two objects are equal according to there implementation of their equals() method, the assertEquals() method will return normally. Otherwise, the assertEquals() method will throw an exception, and the test will stop there.
如果根据它们的 equals() 方法的实现,两个对象相等,则 assertEquals() 方法将正常返回。否则, assertEquals() 方法将抛出异常,测试将停止。
assertSame() and assertNotSame()The assertSame() and assertNotSame() methods tests if two object references point to the same object or not. It is not enough that the two objects pointed to are equals according to their equals() methods. It must be exactly the same object pointed to.
assertSame() 和 assertNotSame()assertSame() 和 assertNotSame() 方法测试两个对象引用是否指向同一个对象。根据它们的 equals() 方法,指向的两个对象相等是不够的。它必须与指向的对象完全相同。
Here is a simple example:
这是一个简单的例子:
@Test
public void assertSame_assertNoSame_example() {
assertSame(employeeService.getEmployeeFromId(1), employeeService.getEmployeeFromId(1));
assertNotSame(employee, employeeService.getEmployeeFromId(1)); // We will get null as response
}