Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error received using easymock

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)
like image 966
jtoepfer Avatar asked Nov 12 '22 23:11

jtoepfer


1 Answers

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(...);
like image 71
aymeric Avatar answered Nov 15 '22 13:11

aymeric