Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assertEquals v/s assertSame method

 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference b/w assertEquals & assertSame method .
thanks .
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assertEquals(java.lang.Object expected, java.lang.Object actual)
     Asserts that two objects are equal.
...
assertSame(java.lang.Object expected, java.lang.Object actual)
     Asserts that two objects refer to the same object.
From JUnit API
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34420
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pooja,
In particular, assertEquals() calls the objects equals() method. If this method was not implemented, it does the same thing as assertSame(). If equals() was implemented, it uses the custom equality test.
 
Charles Hasegawa
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Pooja,
In particular, assertEquals() calls the objects equals() method. If this method was not implemented, it does the same thing as assertSame(). If equals() was implemented, it uses the custom equality test.


Almost true. you can't say "If this method was not implemented, it does the same thing as assertSame()". The default behavior of Object.equals(Object) is to do ==. If a class doesn't override the method, then the default behavior is realized, however if the class under test extends a type that does override the .equals() method, but itself doesn't, it will inherit the .equals of the parent type (which is not likely to be a == check).

So, in a nutshell:
assertEquals() uses the .equals() method for java Objects and == for primitives.
assertSame() uses == to verify that two objects point at the same thing.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34420
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Hasegawa:
Almost true. you can't say "If this method was not implemented, it does the same thing as assertSame()". The default behavior of Object.equals(Object) is to do ==. If a class doesn't override the method, then the default behavior is realized, however if the class under test extends a type that does override the .equals() method, but itself doesn't, it will inherit the .equals of the parent type (which is not likely to be a == check).

Fair enough!
 
Arash Rakhshan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to share my test experience on the subject. I have the following test case:
Please note that I do have a company with "someCompany" name in db with an id of (1L)

Both commented out test cases fails. My assumption was that, JVM assigns the same memory slot for string literals that are the same. I still stand corrected but the issue here is that I am testing two different memory location with contain the same string literal. The assertEquals work and assertSame fails obviously
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic