I have a strange problem with EasyMock
This is the call I’m making which throws an IllegalStateException : Matcher expected as expected
expect(this.mock.expectedOperation(gt(0l), MyClass.class)).andReturn(createClassObject());
If I replace the above call with
expect(this.mock.expectedOperation(gt(0l), createClass(MyClass.class))).andReturn(createClassObject());
@SuppressWarnings("unchecked")
public static <T> Class<T> createClass(Class<T> clazz)
{
return (Class<T>) EasyMock.anyObject();
}
Most times i don’t get an error, but sometimes I do. It stays IllegalStateException : Matcher expected ..
Sometimes I get the IllegalStateException : 2 Matchers expected 1 recorder error for doing this:
MyClass object = createClassObject();
expect(this.mock.expectedOperation(anyLong(), anyLong()).andReturn(object);
public MyClass createClassObject() {
// Actually sets properties and then returns
return new MyClass();
}
But it runs when I do this:
expect(this.mock.expectedOperation(anyLong(), anyLong()).andReturn(createClassObject());
In the above example, sometimes the former runs and latter fails.
Sometimes this fails :
MyClass object = createClassObject();
expect(this.mock.expectedOperation(1, MyClass.class)).andReturn(object);
I have quadruple checked the reset, replay, verify calls. These tests run sometimes and sometimes fail.
If i run my unit test, it randomly fails at least once in one of the above listed situations. Why? How do I get it running?
EDIT : I’m using EasyMock version 3.1 and
MockedClass mock = EasyMock.createMock(MockedClass.class);
Found the problem. We can’t use the gt(0) etc methods to pass parameters into the unit being tested.
In another test by my mistake had used :
service was not a mock, but the unit i was testing.
On checking the documentation I saw that gt(0l) returns 0, which was causing this test to pass by others to fail. (Dunno why?) Since tests are executed randomly, random conditions were failing.
Using the
reset(mocks..)at the beginning of the tests did nothing to help.