Skip to content

EasyMock IllegalStateException

I had a case this morning reworking a test after I’d modified a method signature, from:

        expect(mockRestProxy.getProxiedResource((String) anyObject()))
            .andReturn("{policies}");

to

        expect(mockRestProxy.getProxiedResource((String) anyObject(), userIdentity))
            .andReturn("{policies}");

I received a stacktrace like this:

java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
	at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:41)
	at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:33)
	at org.easymock.internal.ExpectedInvocation.<init>(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)
	at $Proxy0.getProxiedResource(Unknown Source)
...

To quote the easymock documentation,

To match an actual method call on the Mock Object with an expectation, Object arguments are by default compared with equals(). This may lead to problems.

this describes the problem.

The solution appears to be here:

If you would like to use matchers in a call, you have to specify matchers for all arguments of the method call.
There are a couple of predefined argument matchers available.

My solution was in fact to use one of the predefined matchers:

        expect(mockRestProxy.getProxiedResource((String) anyObject(), (UserIdentity) anyObject()))
            .andReturn("{policies}");

One thing I don’t really understand is why EasyMock wouldn’t allow you to match the method signature with a combination of matchers and objects. I wouldn’t think the logic for a heterogenous object equals check would be that terribly complex.

The EasyMock Mockumentation (just a play on words, it’s really not too bad).

Hat Tip to Marcels Javanotes

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*