4

With my mocks in place, I get the error:

redisDao.someMethod(notNull(), notNull()): expected 1, actual 0

If I remove the mock setup, then when tracing in debug mode, it goes to the method redisDao.someMethod and then fails with a null pointer exception.

This doesn't make sense to me, and not sure how to fix this?

mockMaker = EasyMock.createStrictControl();
redisDaoMock = mockMaker.createMock(redisDao.class);

userService.setRedisDao(redisDaoMock);

expect(redisDaoMock.someMethod(EasyMock.<String>notNull(), EasyMock.<String>notNull())).andReturn(someReturn);

mockMaker.replay();
mockMaker.verify();

userController.get(request, response);

// assertions here
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • 3
    I am not completely sure, but shouldn't you put the replay and verify or just verify after the method call? –  Jan 05 '12 at 04:43
  • Good catch, I think verify() does go after the method call. It verifies that what you expected was called. – Logan Jan 05 '12 at 04:50

1 Answers1

10

I think your problem is that you are creating a "Strict" mock object and it expects you to call the method you put in the expect(). If your code does not execute the expected method call, it gives you the expected 1 actual 0 message.

You can use EasyMock.createNiceMock() instead of createStrictControl and this should go away. Or just make sure your code calls the method you tell it in the Expect call.

You also may need to add a .anytimes() to the end of your expect call, so that even if it isn't called, it would be ok.

This page has some good explanations and even has your error

Logan
  • 2,369
  • 19
  • 20
  • 1
    +1 adding .anytimes() at different method calls belonging in the same mock object did the trick for me. – user1563633 Dec 03 '13 at 11:14
  • 1
    https://web.archive.org/web/20151021232918/http://today.java.net/pub/a/today/2006/06/20/getting-started-with-easymock-2.html – Jon Feb 20 '19 at 14:19