Discussion:
What is/are the difference(s) between assertSame and assertEquals in JUnit?
(too old to reply)
RC
2008-08-06 17:53:11 UTC
Permalink
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
2008-08-06 18:31:18 UTC
Permalink
assertSame compares using == (object identity). assertEquals compares
using .equals(). Most tests require the latter.
Mike Schilling
2008-08-06 18:31:43 UTC
Permalink
Post by RC
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
2008-08-06 18:54:33 UTC
Permalink
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());
Post by Mike Schilling
Post by RC
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).
Mike Schilling
2008-08-06 19:12:00 UTC
Permalink
Post by RC
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
2008-08-06 19:18:43 UTC
Permalink
Post by RC
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.
To rephrase, because

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

but

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

--
Lew
Eric Sosman
2008-08-06 20:00:50 UTC
Permalink
Post by RC
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 :-)
--
***@sun.com
Arne Vajhøj
2008-08-07 00:49:42 UTC
Permalink
Post by RC
Post by Mike Schilling
Post by RC
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
2008-08-09 01:25:42 UTC
Permalink
Post by Arne Vajhøj
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
2008-08-11 05:45:10 UTC
Permalink
Post by Arne Vajhøj
Post by RC
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.
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
a***@securifi.com
2017-04-28 14:04:33 UTC
Permalink
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
2017-04-28 15:03:34 UTC
Permalink
Post by a***@securifi.com
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?
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.
--
***@comcast-dot-net.invalid
Thirteen hundred sixty-three days to go.
o***@gmail.com
2019-03-21 11:58:20 UTC
Permalink
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
Post by RC
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)
Loading...