Java EasyMock 空方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22831523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:10:50  来源:igfitidea点击:

EasyMock void method

javaunit-testingtestingjuniteasymock

提问by FireEnigmaX

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 the following in my setup of my test.

我正在尝试使用 EasyMock 来模拟一些数据库接口,以便我可以测试包装方法的业务逻辑。通过在我的测试设置中使用以下方法返回的方法,我一直没问题。

DBMapper dbmapper = EasyMock.createMock(DBMapper.class);
userService.setDBMapper(dbmapper);        

then within my actual test I run

然后在我的实际测试中我运行

EasyMock.expect(dbmapper.getUser(userId1)).andReturn(mockUser1);
EasyMock.replay(dbmapper);
userService.getUser(userId1);

This service then connects to the dbmapper and returns the object (the mapper is injected using setter methods)

该服务然后连接到 dbmapper 并返回对象(使用 setter 方法注入映射器)

These type of mocks seem to work fine. However when I try to run a test for

这些类型的模拟似乎工作正常。但是,当我尝试运行测试时

userService.addUser(newUser1);

This method calls a void method.

此方法调用一个 void 方法。

dbmapper.createUser(newUser);

It's this method that I'm having problems mocking out. I've tried the following

我在模拟这种方法时遇到了问题。我试过以下

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

as some other posts/questions etc seem to suggest I get an IlligalStateException: no last call on a mock available

因为其他一些帖子/问题等似乎表明我得到了 IlligalStateException: no last call on a mock available

Can anyone point me in the right direction please?

任何人都可以指出我正确的方向吗?

Many Thanks in advance

提前谢谢了

采纳答案by Dan Temple

You're close.

你很接近。

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

您只需要在调用之前调用模拟上的方法 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.

这是有效的,因为模拟对象在调用 之前处于记录模式replay(),因此对它的任何调用都将执行默认行为(返回空值/什么都不做),并且在replay()调用该方法时将有资格进行重播。

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

回答by RakeshK

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

如果您使用“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.

有两种模拟——严格和友好。如果调用了意外方法,则严格模拟会抛出断言错误。nice 模拟允许对模拟进行意外的方法调用。

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

有关更多详细信息,请参阅官方文档 - http://easymock.org/user-guide.html#mocking-strict