EasyMock giving unexpected results, says expected 1, actual 0

20,050

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

Share:
20,050
Blankman
Author by

Blankman

... .. . blank

Updated on July 09, 2022

Comments

  • Blankman
    Blankman almost 2 years

    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