Easymock: matcher calls were used outside expectations

12,685

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.

Share:
12,685
Lonzak
Author by

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, 2022

Comments

  • Lonzak
    Lonzak almost 2 years

    I changed the return value of a method in my code from void to and Object. Then two junit test failed stating that an expect(...).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
    Vihar almost 9 years
    isnt the when() a Mockito method rather than easymock one?
  • Karrde
    Karrde almost 9 years
    yeah it's Mockito, I assume theres an equivalent in easymock though