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:
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
arguments of the method call."
Bottom line: read the documentation.
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.This exception is thrown when executing
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)
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: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
Unexpected method call setAttribute("bean", {test=Let's see if this tst value comes through?}):
setAttribute("bean", "something"): expected: 1, actual: 0
arguments of the method call."
Bottom line: read the documentation.
23 comments:
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.
+1
Thanks pal !
Was useful to us also. Thanks.
Thanks, this helped me find my problem as well.
thanks! was searching for a solution for a two days before i found this.
Thanks. This helped me too to solve a similar problem in seconds.
Thanx, we experienced the same problem.
the same problem, thanks for the saved hours!
You're some sort of cross between a genius and a saint. Thanks :)
thank!
helpful...
Yours was the first result, when I did a Google search for this problem. Got the solution right away :).
Thanks !
Glad to be of any help.
Good to see that this post has helped so many people.
thank you!
Thanks, saved me some time !!!
Thank you sir... you and Google have save me some hair :)
+1 again
And another one. Thanks!
Thanks yet again :-)
+1 Thanks!
Thanks! This helped me!!
Your explanation helped a lot. Uptil now i had not been able to understand the cause of this issue for quite some time.
Thanks :)
thanks so much for fixing my issue... wish I could leave you some karma or something :P
Post a Comment