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.

你可能感兴趣的:(EasyMock and IllegalStateException)