Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is/are the difference(s) between assertSame and assertEquals in JUnit?

8,004 views
Skip to first unread message

RC

unread,
Aug 6, 2008, 1:53:11 PM8/6/08
to
In JUnit Assert class have

assertSame(Object expect, Object actual)
and
assertEquals(Object expect, Object actual)

What is/are the difference(s)?

Interesting, there is

assertNotSame(Object expect, Object actual)

But there is no

assertNotEquals(Object expect, Object actual)

softwarepearls_com

unread,
Aug 6, 2008, 2:31:18 PM8/6/08
to
assertSame compares using == (object identity). assertEquals compares
using .equals(). Most tests require the latter.

Mike Schilling

unread,
Aug 6, 2008, 2:31:43 PM8/6/08
to
RC wrote:
> In JUnit Assert class have
>
> assertSame(Object expect, Object actual)
> and
> assertEquals(Object expect, Object actual)

presumable the first checks

expect == actual

and the second checks

expect..equals(actual).

or possibly

expect == null ? actual == null : expect..equals(actual).


RC

unread,
Aug 6, 2008, 2:54:33 PM8/6/08
to

Please help me understand more

StringBuilder s1 = new StringBuilder("abc");
StringBuilder s2 = new StringBuilder("abc");

// Why this tells me they are NOT the same?
Assert.assertSame(s1.toString(), s2.toString());

// Why this tells me they are equals?
Assert.assertEquals(s1.toString(), s2.toString());

Mike Schilling

unread,
Aug 6, 2008, 3:12:00 PM8/6/08
to
RC wrote:
> Please help me understand more
>
> StringBuilder s1 = new StringBuilder("abc");
> StringBuilder s2 = new StringBuilder("abc");
>
> // Why this tells me they are NOT the same?
> Assert.assertSame(s1.toString(), s2.toString());
>
> // Why this tells me they are equals?
> Assert.assertEquals(s1.toString(), s2.toString());
>

They are different objects, but they have the same value. If that's
not enough of an explanation, you need to find a good beginning
tutorial on Java's object model.


Lew

unread,
Aug 6, 2008, 3:18:43 PM8/6/08
to
On Aug 6, 3:12 pm, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:

To rephrase, because

s1.toString() != s2.toString()

but

s1.toString().equals( s2.toString() )

--
Lew

Eric Sosman

unread,
Aug 6, 2008, 4:00:50 PM8/6/08
to
RC wrote:
>
> Please help me understand more
>
> StringBuilder s1 = new StringBuilder("abc");
> StringBuilder s2 = new StringBuilder("abc");
>
> // Why this tells me they are NOT the same?
> Assert.assertSame(s1.toString(), s2.toString());
>
> // Why this tells me they are equals?
> Assert.assertEquals(s1.toString(), s2.toString());

Put a dollar in your left pocket, and a dollar in your right.
Now answer two questions:

1) Is the value in your left pocket equal to the value in
your right pocket?

2) Are your left pocket and your right pocket the same
pocket?

(If your answer to [2] is "Yes," it follows that you have only
one dollar and should send me the other one :-)

--
Eric....@sun.com

Arne Vajhøj

unread,
Aug 6, 2008, 8:49:42 PM8/6/08
to
RC wrote:
> Mike Schilling wrote:
>> RC wrote:
>>> In JUnit Assert class have
>>>
>>> assertSame(Object expect, Object actual)
>>> and
>>> assertEquals(Object expect, Object actual)
>>
>> presumable the first checks
>>
>> expect == actual
>>
>> and the second checks
>>
>> expect..equals(actual).
>>
>> or possibly
>>
>> expect == null ? actual == null : expect..equals(actual).
> Please help me understand more
>
> StringBuilder s1 = new StringBuilder("abc");
> StringBuilder s2 = new StringBuilder("abc");
>
> // Why this tells me they are NOT the same?
> Assert.assertSame(s1.toString(), s2.toString());
>
> // Why this tells me they are equals?
> Assert.assertEquals(s1.toString(), s2.toString());

Maybe assertSame should have been called assertSameObject.

It would have made the meaning of it more obvious.

Arne

Lew

unread,
Aug 8, 2008, 9:25:42 PM8/8/08
to
Arne Vajhøj wrote:
> Maybe assertSame should have been called assertSameObject.
>
> It would have made the meaning of it more obvious.

Perhaps it would have, but I don't see it. How about 'assertIsSame()'? This
extends the convention that "isX" expresses a boolean condition "X", and
avoids the noise-word "Object" in the method name.

Thought experiment only, since no one will change it for our convenience.

--
Lew
f-u set to clj.programmer

Robert Klemme

unread,
Aug 11, 2008, 1:45:10 AM8/11/08
to

I do wonder from time to time why people bother to write documentation.
This whole thread probably would have been avoided by looking at the
JUnit JavaDocs and / or experimenting a bit.

Just my 0.02 EUR...

robert

anvesh.b...@securifi.com

unread,
Apr 28, 2017, 10:04:36 AM4/28/17
to
I have a similar problem

Genericindex a;
Genericindex b;

a= datafrommethod();//returns object of Genericindex
b= datafrommethod();//returns object of Genericindex

b= anothermethod(b);//some object might be overriden

if(notoverridden){
assertEquals(a,b);}// this fails

can someone explain this?

Eric Sosman

unread,
Apr 28, 2017, 11:03:44 AM4/28/17
to
There's not enough information for a full explanation -- in
particular, we don't know what datafrommethod() does, nor what
anothermethod() does. However, if the assertEquals() reports a
failure, what we *do* know is

1) At least one of `a' and `b' is non-null, because if both
were null assertEquals() would not fail. (It considers two
null references to be "equal.")

2) If `a' is null then `b' is non-null. Similarly, if `b'
is null then `a' is non-null. Either way, `a' and `b' are
unequal: One points to an object, and the other is null.

3) If both `a' and `b' are non-null, then they point to
two objects that are not equal according to the equals() method.
This might be the equals() method of GenericIndex itself, or of
a subclass of GenericIndex (if `a' or `b' points to a subclass
instance), or an equals() method that GenericIndex inherits
from one of its superclasses (perhaps java.lang.Object).

... and that's really all I can discern from what you've
provided. If I knew more about about datafrommethod(), about
anothermethod(), and about GenericIndex and its sub- and
super-classes, I might be able to explain more fully.

--
eso...@comcast-dot-net.invalid
Thirteen hundred sixty-three days to go.

onlyfull...@gmail.com

unread,
Mar 21, 2019, 7:58:23 AM3/21/19
to
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
}
You can follow below url to get more information: https://onlyfullstack.blogspot.com/2019/02/junit-assert-methods.html

https://onlyfullstack.blogspot.com/2019/02/junit-tutorial.html
0 new messages