Object reflection for testing ?

69 views
Skip to first unread message

Jan Goyvaerts

unread,
Sep 14, 2012, 7:13:47 AM9/14/12
to The Java Posse
For testing purposes I'm using Unitil's utility classes to compare extensive graph models. Rather than spell out the logic to navigate the tree. More precisely to compare a complex JPA entity graph before and after saving. To make sure it survived persisting to the database.

However, it gets into trouble when comparing collections and arrays. When the comparison is said to be "lenient", it compares without taking the order into account. And of course when it isn't lenient it does take the order into account.

There's a problem with this approach: When being lenient it has to decide how to relate the collections elements to each other. It has no concept of keys. And sometimes it plainly fail. When being non-lenient, the arrays and List are compared fine. But then the Set are failing sometimes. Because the order is not guaranteed. And sometimes the order happen to be the same. And sometimes not.

The bottom line is that it reports a failed test while in fact there isn't. The data only happened to be in a different order. 

---

A solution to this is to make the relationships with a Map. In that case it seems to be able to relate the right collection elements (with the same key). But then I'll have to make all my relationships with maps. On the other hand, there is indeed a kind of secondary key...

I'm a bit puzzled here what to do with it: Change it to Map, look for a different library, ...


Somebody ever had to break his/her head on this kind of problem ?


TIA  -  Jan

 

Casper Bang

unread,
Sep 14, 2012, 8:40:35 AM9/14/12
to java...@googlegroups.com
Not sure I understand what you are trying, but isn't it fundamentally the problem of how to serialize an object graph?

Jason Whaley

unread,
Sep 20, 2012, 8:36:31 PM9/20/12
to java...@googlegroups.com
Can you show a short self contained example of what is failing that should be passing that demonstrates what in unitils you are trying to do?  

Without knowing anything about unitils, is there a way to specify that subclasses of Collection use something like CollectionUtils.isEqualCollection from commons-collections (which I think would work in your case if you can have that invoked for equality tests on Collections)?
Reply all
Reply to author
Forward
0 new messages