Thursday, March 29, 2007

EasyMock and IllegalStateException

When writing a portlet and trying to unit test it, you definitly need somekind of mock implementation. I chose to use EasyMock 2.2 for that purpose because I think its easier to use. I will not explain nor defend this decision.

I have been using EasyMock for some time now, and every now and then I stumble over the problem IllegalStateException problem. Sometimes this is caused by not having 'replay'ed the mocks, but now its different. The stack look s like this:
java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:41)
at org.easymock.internal.ExpectedInvocation.(ExpectedInvocation.java:33)
at org.easymock.internal.ExpectedInvocation.(ExpectedInvocation.java:26)
at org.easymock.internal.RecordState.invoke(RecordState.java:64)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:24)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:45)
This exception is thrown when executing request.setAttribute(MetaInfoPortlet.BEAN_ATTR, EasyMock.anyObject());.

So why is this excpetion being thrown at me? The message gives a hint, 2 matchers expected, recorded 1. So it lacks a matcher...

If I now change the code to request.setAttribute(EasyMock.matches(MetaInfoPortlet.BEAN_ATTR), EasyMock.anyObject());, it works fine. Now why is this? If I change the code to request.setAttribute(MetaInfoPortlet.BEAN_ATTR, "something");, it works as well, but now I get the following:
java.lang.AssertionError:
Unexpected method call setAttribute("bean", {test=Let's see if this tst value comes through?}):
setAttribute("bean", "something"): expected: 1, actual: 0
So it looks like the you either have to supply your mock with fixed values or you have to supply it with matchers, as it says in EasyMocks documentation: "If you would like to use matchers in a call, you have to specify matchers for all
arguments of the method call.
"

Bottom line: read the documentation.

23 comments:

Anonymous said...

Thanks for your post. I was having this problem and had not read the line you quoted from the doco about all arguements needing a matcher.

Anonymous said...

+1

Thanks pal !

Anonymous said...

Was useful to us also. Thanks.

Anonymous said...

Thanks, this helped me find my problem as well.

Anonymous said...

thanks! was searching for a solution for a two days before i found this.

Anvar said...

Thanks. This helped me too to solve a similar problem in seconds.

Skala said...

Thanx, we experienced the same problem.

Anonymous said...

the same problem, thanks for the saved hours!

Andrew C said...

You're some sort of cross between a genius and a saint. Thanks :)

fuz said...

thank!

Anonymous said...

helpful...

Anonymous said...

Yours was the first result, when I did a Google search for this problem. Got the solution right away :).

Thanks !

SpiderMars said...

Glad to be of any help.
Good to see that this post has helped so many people.

foo said...

thank you!

akochnev said...

Thanks, saved me some time !!!

Anonymous said...

Thank you sir... you and Google have save me some hair :)

Anonymous said...

+1 again

Anonymous said...

And another one. Thanks!

Andrew Clegg said...

Thanks yet again :-)

Jan Z said...

+1 Thanks!

juco said...

Thanks! This helped me!!

bienvenuearanade said...

Your explanation helped a lot. Uptil now i had not been able to understand the cause of this issue for quite some time.

Thanks :)

Elizabeth said...

thanks so much for fixing my issue... wish I could leave you some karma or something :P