Why do assertEquals and assertSame in junit return the same result for two instances of the same class?

According to the documentation

assertEquals () Asserts that two objects are equal.

assertSame () Asserts that two objects reference the same object.

So I expect that if I have a class as shown below

class SomeClass {} 

then

 SomeClass someClass1= new SomeClass(); SomeClass someClass2= new SomeClass(); assertSame(someClass1,someClass2); // fail assertEquals(someClass1,someClass2); // fail 

assertEquals should pass, and assertSame should fail, because the values ​​of both classes are equal, but they have a different reference location.

Since in both cases I get a failure, the question arises: what is the difference between the two?

+12
source share
6 answers

Since you have not redefined peers in your class, assertEquals behaves the same as assertSame , since the default value is equal to comparing implementation comparison references.

 150 public boolean equals(Object obj) { 151 return (this == obj); 152 } 

If you provide a silent redefinition of peers:

 class SomeClass { @Override public boolean equals(Object o) { return true; } } 

you will see that assertEquals succeeds.

+21
source

assertEquals uses the equals() method (which you must override in your class to really compare its instances) to compare objects, and assertSame uses the == operator to compare them. So the difference is exactly the same as between == (by value) and equals (compare identifier).

+9
source

JUnit official documentation:

assertEquals: claims that two objects are equal .

assertSame: claims that two objects belong to the same object .

In other words

assertEquals: uses the equals () method, or if the equals () method has not been overridden, compares the link between two objects.

assertSame: compares a link between two objects.

Example 1 : the equals method was not overridden, so assertSame and assertEquals return the same result as they compare the reference to objects.

 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 : the equals method is overridden, so assertSame and assertEquals do not return the same result, because this time the equals method will be used by assertEquals.

 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 } } 
+2
source

The first statement fails because someClass1 and sameClass2 are not the same instances. The second statement fails because the equals(Object) method was not defined in SomeClass , and its super equals(Object) refers to equality. Since two different instances are compared for equality, this happens for the same reason as the first.

0
source

assertEquals - It checks that objects are equal or not based on the overridden equals () method of this class. Thus, we can check the equality of objects depending on their state (compare the values ​​of the variables of their instances).

assertEquals () The assertEquals () method compares two objects for equality using their equals () method.

 @Test public void assertEquals_example() { Employee employeeNew = new Employee(); employee.setSalary(1000000.0); assertEquals("EMPLOYEE OBJECT", employee, employeeNew); } 

If two objects are equal according to the 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.

assertSame () and assertNotSame () The assertSame () and assertNotSame () methods check if two references point to the same object. It is not enough that the two specified objects are equal according to their equals () methods. It must be exactly the same object that it points to.

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 } 
0
source

assertEquals: ==
assertSame: ===

"same" matches the type with a value that matches "===".

-2
source

Source: https://habr.com/ru/post/982315/


All Articles