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

会有一股神秘感。 提交于 2019-12-20 11:36:08

问题


According to documentation

assertEquals() Asserts that two objects are equal.

assertSame() Asserts that two objects refer to the same object.

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.

As I get failure in both cases then my question is what are the difference between these two ?


回答1:


Since you didn't override equals in your class, assertEquals behaves the same as assertSame since the default equals implementation compare references.

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

If you provide a dumb overriding of equals:

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

you'll see that assertEquals succeeds.




回答2:


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).




回答3:


Official JUnit documentation:

assertEquals: Asserts that two objects are equal.

assertSame: Asserts that two objects refer to the same object.

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.

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

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.

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



回答4:


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.




回答5:


assertEquals: ==
assertSame: ===

'same' matches the type along with the value which is the same as '==='.




回答6:


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() 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 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.

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.

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
}


来源:https://stackoverflow.com/questions/28451925/why-assertequals-and-assertsame-in-junit-return-the-same-result-for-two-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!