Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a custom message from a custom Mockito ArgumentMatcher?

I'm writing an ArgumentMatcher and the guts of the comparison come down to something like:

return A.value().equals(B.value()) && A.name().equals(B.name());

Unfortunately, when the doesn't pass, Mockito just tells me it failed. I want to add a custom message like "Values don't match" or "Names don't match" (of course I'd like to give more info, but until I can figure out this simple case, what's the point of going any further).

Previously (before working with Mockito), I remember matchers having two methods - one to check the match and one to generate a failure message (true, it was a pain to write both methods, but I miss the second method now).

Any idea how to do this? Any help is appreciated!

like image 280
Todd R Avatar asked Feb 19 '11 15:02

Todd R


1 Answers

I get it now. Hamcrest provides a "describeTo" method. This is equivalent to the method I remember from EasyMock. You simply add your error conditions to the Description object, and viola, you have a better failure message.

like image 112
Todd R Avatar answered Sep 30 '22 11:09

Todd R