Error received using easymock

I am working on a new project and they are using EasyMock (v2.4), which I am not familiar with. I need to be able to do the following, but no one has an answer. The current structure is used BaseDao.class, which I would like to mock 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 am 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)
+5
source share
3 answers

You define replay(...)twice, therefore only the first is taken into account. It is so defined until you name it reset(...).

To fix the problem, you can:

1) Delete the call causing the test to fail:

EasyMock.expecting(baseDao.findByNamedQuery("def.query"), EasyMock.anyLong)
   .andReturn(...);
EasyMock.replay(baseDao);

2) Instead of defining a fixed row while waiting, you can expect any row:

EasyMock.expecting(baseDao.findByNamedQuery((String)EasyMock.anyObject()), 
   EasyMock.anyLong).andReturn(...);
+1

, "abc.query", "def.query".

.

0

If you expect findByNamedQuery to be called twice, remove the 1st call to the retry method. This is necessary only once, because all 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);
0
source

All Articles