EasyMock’s “N matches expected, M recorded” not always what you expect

This has been discussed in varying ways. I tend to point to this article as a good description of how to understand what is going on when this message is given. There is an interesting corner case that I feel needs to be explored as I’ve just wasted an hour trying to figure it out.

Consider the following code sample.

public void testSomethingBasic() {
  Thing myThing = new Thing(EasyMock.isA(Whatever.class));
  // do more stuff in the test
}
public void testSomethingElse() {
  IJobber myJobber = EasyMock.createMock(IJobber.class);
  EasyMock.expect(myJobber.doStuff()).andReturn(null);
  // do more stuff in the test
}

The thing to notice is that isA(..) was used on a non-mock object then used again on a mock object. The problem here is that EasyMock will record that you created a matcher (ie. isA(Whatever.class)) that was never used then will record that you created another one that you did use. You’ll get an error to the nature of “1 matcher expected, 2 recorded”.

While this is not always the case, a debugging method to consider is when you have more recorded than expected, check test methods that run before the one throwing this exception to make sure you aren’t improperly creating matchers that don’t get used.

Advertisements

One thought on “EasyMock’s “N matches expected, M recorded” not always what you expect

  1. Hi

    Thanks for pointing this out mate . I had a few hours of head scratching until I came this post. I checked out the link above but that didnt explain why I was getting the error until I came across this post so thanks for the good work!. You’ve saved me a few more hours of head scratching.

    quophyie

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s