Tuesday, January 28, 2014

Easymock: matcher calls were used outside expectations

You are running your unit tests with EasyMock and then you face this kind of error "matcher calls were used outside expectations".

java.lang.IllegalStateException: matcher calls were used outside expectations
at org.easymock.internal.RecordState.replay(RecordState.java:86)
at org.easymock.internal.MocksControl.replay(MocksControl.java:169)


This happens because you are having some kind of code like this, where the .andReturn is not returning an object.
expect(mockdao.bark(EasyMock.anyObject(Dog.class))).andReturn(EasyMock.anyObject(Noise.class)));

To fix this, you should use the null return or return an instance of an object, or even a mock object:
expect(mockdao.bark(EasyMock.anyObject(Dog.class))).andReturn(new Noise()));

9 comments: