Custom matcher as mockito argument matcher: how to get correct message

146 views
Skip to first unread message

Barry Kaplan

unread,
May 27, 2010, 8:03:54 PM5/27/10
to specs-users
I have defined a very simple matcher:

def ==(expected: CycleInterval) = new Matcher[CycleInterval] {
def apply(a: => CycleInterval) = {
val actual = a
(actual.getClass == expected.getClass && actual.deviceId ==
expected.deviceId && actual.interval == expected.interval,
actual.toString + " is same as " + expected.toString,
actual.toString + " is not same as " + expected.toString)
}
}

And use it like:

@Test def adds_in_cycle_interval {
detector.inCycleHandler must notBeNull
detector.inCycleHandler(deviceId)
timeline.add(==(InCycleInterval(deviceId, fromNow))) was called
}

But when it fails I get a message where the expected argument to the
call seems to be the empty string. What would be needed in my matcher
so that the actual argument value is displayed?

org.specs.specification.FailureExceptionWithResult: The method was not
called as expected:
Argument(s) are different! Wanted:
mongoDiscontinuousTimeline.add(

);
-> at systeminsights.module.oee.CycleTimelineBuilderSpec$$anonfun
$adds_scheduled_downtime_interval
$3.apply(CycleTimelineBuilderSpec.scala:58)
Actual invocation has different arguments:
mongoDiscontinuousTimeline.add(
ScheduledDowntimeInterval(did,
1969-12-31T16:00:00.000/292278994-08-17T00:12:55.807,4bff06b9da9db946136f7add)
);
-> at systeminsights.module.oee.CycleTimelineBuilder$$anonfun
$3.apply(CycleTimelineBuilder.scala:26)

etorreborre

unread,
May 27, 2010, 8:43:42 PM5/27/10
to specs-users
Hi Barry,

My gut feeling, after having a look at my code, is that this is a bug.
Can you please fill out an issue and I'll try to fix it later on
today?

Thanks.

etorreborre

unread,
May 28, 2010, 12:28:53 AM5/28/10
to specs-users
I have fixed the issue by displaying the ok message of the matcher
when there's a failure.

In your case that will give:

Argument(s) are different! Wanted:
mongoDiscontinuousTimeline.add(actual is same as expected)
);

Actual invocation has different arguments:
mongoDiscontinuousTimeline.add(
ScheduledDowntimeInterval(did,
1969-12-31T16:00:00.000/292278994-08-17T00:12:55.807,4bff06b9da9db946136f7a
dd)
)

That may not be the best message in your case but that's the only
generic way to deal with any kind of Matcher. Maybe in your case you
can just output the expected value as the "ok message" of your
matcher?

E.
Reply all
Reply to author
Forward
0 new messages