I have class, which defines equals just using id attribute. It also has another method (called "areSame"), which checks if the given argument has some important properties equal to this objects properties (and it disregards id).
In test, I'd like to check, if two collections are same using the areSame as "custom equals" method (preferably ignoring order and without relying on hashCode, which is inconsistent with areSame, obviously).
I was wondering, if there is shorter way then writing a wrapper. It'd be more pleasant to write something like
assertThat(actualCollection,
containsInAnyOrder_butUsingCustomEquals(expectedCollection, "areSame"));
Hamcrests examples, when dealing with collections, only use simple types and while there are possibilities for comparing objects like reflectEquals, I haven't figured out how to combine matchers to do what I want.
I'm using JUnit, so Hamcrest seemed to be first joice as JUnit already contains some of its matchers. But as I'm not tied to it yet, I guess that solution using AssertJ, Mockito or other framework would be acceptable.
So, is this doable in some testing framework or should I wrapp it up?
Since my quite long search didn't reaveal anyone even unsuccessfuly asking about this, I guess most people don't waste time agonizing over few more lines and just write the wrapper and factory to convert collection, right? Hope I haven't missgoogled...
Aucun commentaire:
Enregistrer un commentaire