EasyMock void method

后端 未结 2 1909
执笔经年
执笔经年 2021-01-03 19:01

I\'m trying to use EasyMock to mock out some database interface so I can test the business logic off a wrapping method. I\'ve been going ok with methods that return by using

相关标签:
2条回答
  • 2021-01-03 19:11

    You're close.

    You just need to call the method on your mock before calling expectLastCall()

    So you expectation would look like this:

    userService.addUser(newUser1);
    EasyMock.expectLastCall();
    EasyMock.replay(dbMapper);
    userService.addUser(newUser1);
    

    This works because the mock object is in Record mode before the call to replay(), so any calls to it will perform default behaviour (return null/do nothing) and will be eligible for replaying when the replay() method is called.

    What I like to do to make sure that it is obvious the method call is for an expectation is to put a small comment in front of it like this:

    /* expect */ userService.addUser(newUser1);
    EasyMock.expectLastCall();
    EasyMock.replay(dbMapper);
    userService.addUser(newUser1);
    
    0 讨论(0)
  • 2021-01-03 19:17

    This problem does not happens if you use the 'nice' API:

    DBMapper dbmapper = EasyMock.createNiceMock(DBMapper.class);
    

    There are two kinds of mock - strict and nice. The strict mock throws Assertion Error in case an unexpected method is called. The nice mock allows unexpected method calls on the mock.

    For further details, refer to the official doc - http://easymock.org/user-guide.html#mocking-strict

    0 讨论(0)
提交回复
热议问题