Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unexpected invocation error with mock objects

 
jeff mutonho
Ranch Hand
Posts: 271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method getAccountByAccountKey below is where I'm getting the " unexpected invocation" error

mockLinkedServiceAccountDao = mock(LinkedServiceAccountDao.class);

mockLinkedServiceAccountDao.verify();

linkedServiceAccountDao = (LinkedServiceAccountDao) mockLinkedServiceAccountDao.proxy();

accountId = ((Account) linkedServiceAccountDao.getAccountByAccountKey(ban).get(0)).getId();

System.out.println("## accountId : " + accountId.toString());

mockLinkedServiceAccountDao.expects(once()).method("getAccountByAccountKey").with(eq(new Long(1))).will(returnValue(accountId));


junit ] Testcase: test1 took 0.14 sec

[ junit ] FAILED

[ junit ] mockLinkedServiceAccountDao: unexpected invocation

[ junit ] Invoked: mockLinkedServiceAccountDao.getAccountByAccountKey("1234567890")

[ junit ] Allowed:

[ junit ] No expectations set

[ junit ] org.jmock.core.DynamicMockError: mockLinkedServiceAccountDao: unexpected invocation

[ junit ] Invoked: mockLinkedServiceAccountDao.getAccountByAccountKey("1234567890")

[ junit ] Allowed:

[ junit ] No expectations set

[ junit ] at org.jmock.core.AbstractDynamicMock.mockInvocation( AbstractDynamicMock.java:95

--

What's causing this?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to *first* set the expectations (your last line), and *then* use the mock in your test.

Moving to our testing forum...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic