Do you know how to use JUnit to test whether two Hibernate sessions are the same? Do I need to override equals()
?

- 67,400
- 29
- 193
- 254

- 1,509
- 4
- 22
- 34
-
1What kind of session? Incoming web request is on the same session? Persistent session objects(SSH etc)? – Thomas Dec 18 '12 at 03:24
-
It's hibernate session.Thank you! – typeof programmer Dec 18 '12 at 03:35
2 Answers
If you just want to see if two references are pointing to the same object, then the == operator is enough.
If what you want is to test for equality, i.e. that they have the same variables then you would have to override the equals method with your specific logic to determine equality. But if the objects are of a type that you don't have access to the source to modify, then you'll have to depend on them implementing the equals method in a way that satisfies your requirements.
In those rare cases where it's not what you want, then as long as the class is extendible you can create an Anonymous class that override the equals method.

- 5,254
- 4
- 23
- 28
-
Thanks a lot for your guidance.Could you give me an example of using junit to test equality of two session and override equals()? Thank you! – typeof programmer Dec 18 '12 at 04:21
Check out this question Hibernate Object Equality Checking
From the above ref, JUnit assert should be like:
assertTrue(o1.getClass().equals(o2.getClass()) && o1.getId().equals(o2.getId()));