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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值