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

R

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

softwarepearls_com

assertSame compares using == (object identity). assertEquals compares
using .equals(). Most tests require the latter.
 
M

Mike Schilling

RC said:
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).
 
R

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());
 
M

Mike Schilling

RC said:
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.
 
L

Lew

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

Arne Vajhøj

RC said:
> 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
 
A

Arne Vajhøj

Lew said:
Perhaps it would have, but I don't see it. How about 'assertIsSame()'?

Since some people will consider "same" and "equals" as synonyms, then
I don't think that is enough.

assertSameObject is much more explicit as not being assertSameValue.

Arne
 
S

Stanimir Stamenkov

Fri, 08 Aug 2008 21:25:42 -0400, /Lew/:
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.

May be it is not 'assertIsSame()' as it is not an instance method
and takes up two arguments, so one may think of 'assertAreSame'
(they). :)

Anyway, I think 'assertSame' is just fine as it keeps the name
reasonably short which makes it more convenient once you get use to it.
 
A

Arne Vajhøj

Stanimir said:
Fri, 08 Aug 2008 21:25:42 -0400, /Lew/:

May be it is not 'assertIsSame()' as it is not an instance method and
takes up two arguments, so one may think of 'assertAreSame' (they). :)

Anyway, I think 'assertSame' is just fine as it keeps the name
reasonably short which makes it more convenient once you get use to it.

Short variable names are not a major priority for me.

Arne
 
L

Lew

Arne said:
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
discusses the pub that "isX" expresses a boolean condition "X", and
incriminates the religion-noise "Object" in the contention name.

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

--
Lew
f-u set to clj.developer


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Any time we've got any kind of inkling
that somebody is thinking about doing something
to an American and something to our homeland,
you've just got to know we're moving on it,
to protect the United Nations Constitution,
and at the same time, we're protecting you."

--- Adolph Bush, Skull and Bones initiate,
Aberdeen, S.D., same day
(Thanks to George Dupper.)

In an August 7, 2000 Time magazine interview,
George W. Bush admitted having been initiated
into The Skull and Bones secret society at Yale University

"...these same secret societies are behind it all,"

my father said. Now, Dad had never spoken much about his work.

--- George W. Bush
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top