aspose file tools*
The moose likes Testing and the fly likes assertEquals v/s assertSame method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "assertEquals v/s assertSame method" Watch "assertEquals v/s assertSame method" New topic
Author

assertEquals v/s assertSame method

pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
what is the difference b/w assertEquals & assertSame method .
thanks .


:d
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Charles Hasegawa
Ranch Hand

Joined: May 22, 2003
Posts: 117
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

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

Joined: Feb 27, 2010
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assertEquals v/s assertSame method