Reputation: 325
I am working on a new project and they have been using EasyMock (v2.4), which I am not as familiar with. I need to be able to do the following, but no one has an answer. The current framework uses an BaseDao.class
which I would like to mock out per the following example, but I get an error. I'm looking for some direction.
BaseDao baseDao = EasyMock.mock(BaseDao.class);
EasyMock.expect(baseDao.findByNamedQuery("abc.query"), EasyMock.anyLong()).andReturn(...);
EasyMock.replay(baseDao);
EasyMock.expect(baseDao.findByNamedQuery("def.query"), EasyMock.anyLong).andReturn(..);
EasyMock.replay(baseDao);
The error I'm getting is as follows...
java.lang.AssertionError:
Unexpected method call findByNamedQuery("def.query"):
findByNamedQuery("abc.query", 1): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:61)
at $Proxy5.findByNamedQuery(Unknown Source)
Upvotes: 5
Views: 1783
Reputation: 222
If you are expecting findByNamedQuery to be called twice, then remove the 1st call to the replay method. It is only needed the once, after all you expectations for the test have been set.
BaseDao baseDao = EasyMock.mock(BaseDao.class);
EasyMock.expect(baseDao.findByNamedQuery("abc.query"), EasyMock.anyLong()).andReturn(...);
// Remove EasyMock.replay(baseDao);
EasyMock.expect(baseDao.findByNamedQuery("def.query"), EasyMock.anyLong).andReturn(..);
EasyMock.replay(baseDao);
Upvotes: 0
Reputation: 3895
You are defining a replay(...)
twice so only the first one will count. It's defined like this until you call reset(...)
.
To fix the problem, you can either:
1) Remove the invocation that is causing the test failure:
EasyMock.expecting(baseDao.findByNamedQuery("def.query"), EasyMock.anyLong)
.andReturn(...);
EasyMock.replay(baseDao);
2) Instead of defining a fixed string in your expectation, you can expect any string:
EasyMock.expecting(baseDao.findByNamedQuery((String)EasyMock.anyObject()),
EasyMock.anyLong).andReturn(...);
Upvotes: 1
Reputation: 114787
It looks like the test expected a method call with the parameter "abc.query" but the method was called with "def.query" instead.
Debugging the test step by step should help finding the problem.
Upvotes: 0