• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

junit test and object serialization

 
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello,

I am writing a JUnit test. The objects I am testing contain many other objects. Some of these objects are Maps and Lists that contain other objects.

I want the JUnit tests to test for equality of the objects.

As I see it, there are four ways to go about this:

1) serialize the objects and then compare the byte arrays.

2) call an equals method in the object itself; however, the objecst to be tested have already been written, so I don't think I will be able to add an equals method to the objects.

3) have the JUnit tests perform the equals test themselves by testing each element of the map or list. This would require that the JUnit test knows each of the objects very well, instead of letting this knowledge stay in the object.

4) create a helper object that handles equals for various kinds of objects -- not the best design because this class could grow without bounds as more objects are added.


Thanks for any help that you can give.

Ravi

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's move this to our testing forum.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi,
I wouldn't go with option #1 because it would be very difficult to figure out why the assertion was failing. I also wouldn't go with #2 because that requires writing both the equals and hashCode methods correctly and then testing them.

Option #3 is an option. I would go with option #5 though. Which is to add a toString() method to the object. It's easier to write toString() correctly than equals. It's also easier to see why the objects are different because junit highlights the differences in the string. If you can't update the class at all, you could write the toString elsewhere. But that's awkward. Why can't you change the code? Does another team/company own it or is just phobia of changing "already written" code?
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic