java 使用 EasyMock 模拟一个具体的类

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

Mocking a concrete class using EasyMock

javaeasymock

提问by Samuel Carrijo

Is it possible to mock a concrete class using EaskMock? If so, how do I do it?

是否可以使用 EaskMock 模拟具体类?如果是这样,我该怎么做?

回答by Jon Skeet

See the EasyMock Class Extensiondocumentation and download it from the SourceForge project. You can't mock final methods though.

请参阅EasyMock 类扩展文档并从 SourceForge 项目下载它。但是你不能模拟 final 方法。

EDIT: This is now part of EasyMock for v3 and above, as noted in comments.

编辑:如评论中所述,这现在是用于 v3 及更高版本的 EasyMock 的一部分。

回答by Rich Seller

Powermockextends EasyMock and allows you to mock concrete types, even final and static methods.

Powermock扩展了 EasyMock 并允许您模拟具体类型,甚至是最终和静态方法。

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.

PowerMock 是一个框架,它扩展了其他模拟库,例如具有更强大功能的 EasyMock。PowerMock 使用自定义类加载器和字节码操作来模拟静态方法、构造函数、最终类和方法、私有方法、静态初始化器的删除等。

回答by Mike Partridge

EasyMock class extensions have been integrated into EasyMock as of v3.0; see the release notes.

从 v3.0 开始,EasyMock 类扩展已经集成到 EasyMock 中;请参阅发行说明

回答by Jay Bose

When creating a mock, simply use the org.easymock.classextensionversion of the EasyMock class. The result will be castable.

创建模拟时,只需使用EasyMock 类的org.easymock.classextension版本。结果将是可铸的。

Here's a recent link - http://easymock.org/EasyMock3_2_ClassExtension_Documentation.html

这是最近的链接 - http://easymock.org/EasyMock3_2_ClassExtension_Documentation.html