EasyMock : java.lang.IllegalStateException: 1 matchers expected, 2 recorded

蓝咒 提交于 2019-12-04 22:57:01

I haven't looked meticulously closely yet, but this looks suspect:

String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());

anyObject() is a matcher and you're calling it after the replay. It's not used to produce any object. It's used to instruct EasyMock to allow any object. EasyMock is detecting that extra matcher but it is not harmful until the second test. At that point, the number of matchers that EasyMock has recorded but hasn't yet used (2) doesn't line up with the number of parameters expected for the second doXthing call (1).

You should be passing in real parameters to testCallingXthing (or a mock that is in replay mode). Try passing in null directly, or a real value like 2.

for me this failure (in my case 2 matchers expected, 4 recorded.) meant "you are mixing easymock and mockito in the same unit test, so accidentally calling easymock's notNull() method for a mockito argument. Which causes the failure but only if the tests are run in a certain order.

nsfyn55

Try:

String testResult = mainThing.testCallingXthing(eq(EasyMock.anyLong()));

There are more refined matchers than anyObject(). These allow you to make type-based assertions about collaborators.

From the EasyMock documentation:

eq(X value)
Matches if the actual value is equals the expected value. Available for all primitive types and for objects.
anyBoolean(), anyByte(), anyChar(), anyDouble(), anyFloat(), anyInt(), anyLong(), anyObject(), anyShort()

user1371983

You should reset mock after each test method to get rid of this problem. Adding below code will solve this problem.

@After 
public void after(){
    EasyMock.reset(mockXthing)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!