Saturday, December 10, 2011

JUNIT - java.lang.IllegalStateException: 2 matchers expected, 1 recorded.

So I've been planning to write this down for a long time, finally got sometime to do it.

If you are new to JUnit you would often find yourself facing this error .

java.lang.lllegalStateException: 2 matchers expected, 1 recorded
or
i matchers expected, j recorded.....

Here is the complete trace:

java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
at
org.easymock. internal. Expected Invocation .create MissingMatchers( Expected Invocation .java :56)
at org.easymock. internal. Expected Invocation.<init>( Expected Invocation .java :48)
at org.easymock.internal.Expectedlnvocation.<init>(Expectedlnvocation.java:40)
at org.easymock.internai.RecordState.invoke(RecordState.java:76)
at org.easymock.internal.MocklnvocationHandler.invoke(MocklnvocationHandler.java:38)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:72)
at $Proxy2 .getDataByGroupslist( Unknown Source)
at
..................................
at sun.reflect.NativeMethodAccessorlmpl.invokeO(Native Method)
at sun.reflect.NativeMethodAccessorlmpl.invoke(NativeMethodAccessorlmpl.java:4~)
at sun.reflect.DelegatingMethodAccessorlmpl.invoke(DelegatingMethodAccessorlmpl.java:25)
at java .lang. reflect. Method. invoke( Method .java :600)
at org.junit.runners.modei.FrameworkMethod$1.runReflectiveCaii(FrameworkMethod.java:44)
at org.j unit. intern a l.runners.modei.ReflectiveCa liable .run( ReflectiveCa II a ble.java: 15)
at org.j unit. runners. mode I. Fra mewo rkMethod. invoke Explosively( Framewo rkMethod .java :41)
at org.junit.internal.runners.statements.lnvokeMethod.evaluate(lnvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BiockJUnit4CiassRunner.runChild{BiockJUnit4CiassRunner.java:76)
at org.junit.runners.BiockJUnit4CiassRunner.runChild{BiockJUnit4CiassRunner.java:SO)
at org.junit.runners.ParentRunner$3.run{ParentRunner.java:193)
at org.junit. runners. Pa rentRunner$1.sched ule{Pa rentRu n ner.java :52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$OOO{ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.interna l.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390}
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

How to fix this?

The answer is that "The mock should contain all fixed values or all matchers."
EasyMock documentation (http://easymock.org/EasyMock2_2_Documentation.html) says
"If you would like to use matchers in a call, you have to specify matchers for all arguments of the method call. ."

For Example 
1.  EasyMock.expect(testTableDao.getDataByGroupslist(groups, 1)).andReturn(testTablesRows);   - works
2.  EasyMock.expect(testTableDao.getDataByGroupslist(EasyMock.isA(List.class), EasyMock.isA(Integer.class))).andReturn(testTablesRows);   - works
3.  EasyMock.expect(profileDao.getProfilesByGroupslistAndCntctCntrCode(groups,EasyMock.isA(Integer.class))).andReturn(testTableRows); -doesn't work

The code1 perfectly works fine because both the parameters for the method of the mocked object are fixed values.
The code2 perfectly works fine because both the parameters for the method of the mocked object are matchers.
The code3 would throw the java.lang.lllegaiStateException: 2 matchers expected, 1 recorded  because one parameter is a fixed value and the other is a matcher.

No comments:

Post a Comment