Complex matcher expressions: am I going about this the right way? (JMock 2)

View: New views
1 Messages — Rating Filter:   Alert me  

Complex matcher expressions: am I going about this the right way? (JMock 2)

by Richard Barnett-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi --

Some of my JMock expectations end up using matcher expressions which
seem to me overly complex;  for example

allOf(
    anArrayOfSize(1),
    hasItemInArray(allOf(
        beanEqualIgnoring(lTransferGroup, "members"),
        hasProperty("members", allOf(
            aCollectionOfSize(3),
            hasItems(
                lInviteeContact,
                lTransferGroup.getMembers().get(0),
                lTransferGroup.getMembers().get(1))))
        )))

(which uses a few custom matchers, without which the expression would be
even more complex).  

This is just an assertion that an argument is an array with exactly 1
element, whose bean properties are equal to some expected object
ignoring the order of the 'members' collection.

This could be simplified by adding matchers such as
- hasExactlyItemsInArray(...) [ie combining anArrayOfSize() &
hasItemInArray()]
- a new beanEqual...() which could ignore order in specific collections

The equals() method on the class of lTransferGroup is implemented for
"business equality" (ie 2 groups are equals() if they have the same
unique id, no matter what their member lists are), which is why I'm
comparing bean properties.  

Making equals() strong enough to use in these tests would require
non-test code to use 'unnatural' techniques when dealing with
collections, etc.

So:
- Are there a bunch of more powerful matchers that I should be using to
simplify this?
- If not, should I write them?
- Am I missing something more fundamental?
- Or is the above expression not actually that complex?

One problem of building complex expressions from matchers is that each
one uses a boolean to indicate whether it matched or not;  it's
therefore non-trivial to work out which of the matchers failed.  While
writing matchers (eg the beanEqualIgnoring one above) I found myself
wanting to throw an exception which could contain a description of the
failure.

Help, thoughts & opinions welcome.

-- Richard

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price