java 如何忽略 JUnit/easymock 中意外的方法调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10598543/
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-10-31 01:46:44  来源:igfitidea点击:

How to ignore unexpected method calls in JUnit/easymock?

javajunitmockingeasymock

提问by Rory

I'm just wondering if it is possible using Junit and easymock to ignore unexpected method calls?

我只是想知道是否可以使用 Junit 和 easymock 来忽略​​意外的方法调用?

I.e. instead of the test failing I want to be able to say - "at this point - ignore any unexpected method calls and just continue with the test as normal'

即,我希望能够说,而不是测试失败 - “此时 - 忽略任何意外的方法调用,然后继续正常进行测试”

Thanks

谢谢

回答by mmccomb

With EasyMock you can create a nice mock, which unlike a normal mock object does not throw assertion errors if an unexpected/recorded call occurs. To quote the easymock documentation...

使用 EasyMock,您可以创建一个很好的模拟,与普通模拟对象不同,如果发生意外/记录的调用,它不会抛出断言错误。引用easymock文档...

On a Mock Object returned by createMock() the default behavior for all methods is to throw an AssertionError for all unexpected method calls. If you would like a "nice" Mock Object that by default allows all method calls and returns appropriate empty values (0, null or false), use createNiceMock() instead.

在 createMock() 返回的 Mock 对象上,所有方法的默认行为是为所有意外方法调用抛出 AssertionError。如果您想要一个默认允许所有方法调用并返回适当的空值(0、null 或 false)的“漂亮”模拟对象,请改用 createNiceMock()。

To create a nice mock, use the static createNiceMock(Class class) method on the Easymock class...

要创建一个不错的模拟,请在 Easymock 类上使用静态 createNiceMock(Class class) 方法...

SomeClass someClassNiceMock = EasyMock.createNiceMock(SomeClass.class);

Reference: http://easymock.org/user-guide.html#mocking-nice

参考:http: //easymock.org/user-guide.html#mocking-nice