EasyMock void method

自闭症网瘾萝莉.ら 提交于 2019-11-30 16:53:56

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);
RakeshK

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!