Easymock: matcher calls were used outside expectations
Solution 1
I changed to code to
expect(myMock.foo(1l,FooEnum.A)).andReturn(new Object());
and now it works. It's still strange why I get this error since I definitely return a new Object
(and not null
or anything)
Solution 2
EasyMock.anyObject(String.class)
is a matcher, it isn't a String
and can't be used as a String
except for matching - matching being something like the following:
when(foo.bar(EasyMock.anyObject(String.class))).thenReturn("foo-bar")
P.S. you should avoid using new String()
whenever possible; it's always better to use ""
instead.
Lonzak
All original source snippets I post on stackoverflow are dedicated to the public domain. Do with them as you see fit. #SOreadytohelp
Updated on June 04, 2022Comments
-
Lonzak almost 2 years
I changed the return value of a method in my code from
void
to andObject
. Then two junit test failed stating that anexpect(...).andReturn(...)
was missing. After adding those one test is fixed and the other still throws an exception which seems a bit weird:java.lang.IllegalStateException: matcher calls were used outside expectations
The code which works for one but not the other is:
expect(myMock.foo(1l,FooEnum.A)).andReturn(EasyMock.anyObject(String.class));
Any ideas?
-
Vihar almost 9 yearsisnt the
when()
a Mockito method rather than easymock one? -
Karrde almost 9 yearsyeah it's Mockito, I assume theres an equivalent in easymock though