打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java 各种Mock工具比较

转自:

http://billben.iteye.com/blog/1872196

http://harrywu304.blog.163.com/blog/static/8456603201141611147777/


可用的Mock Toolkit有许多,比较常见的有EasyMock, Jmock和JMockit等等,到底选哪个呢,Jmockit的官网上有个特性对比列表,很详细:

FeatureEasyMockjMockMockitoUnitils MockPowerMock:
EasyMock 
API
PowerMock:
Mockito
 API
JMock
it
Invocation count constraints
Recording strict expectations


Explicit verification


Partial mocking
No method call to switch from record to replay


No extra code for implicit verification

N/AN/A
N/A
No extra "prepare for test" code

No need to use @RunWith annotation or base
 test class



Consistent syntax between void and non-void methods



Argument matchers for some parameters only, 
not all





Easier argument matching based on properties 
of value objects

Cascading mocks


Support for mocking multiple interfaces



Support for mocking annotation types

Partially ordered expectations




Mocking of constructors and final/static/native/private methods



Declarative application of mocks/stubs to 
whole test classes




Auto-injection of mocks


Mocking of "new-ed" objects



Support for mocking enum types



Declarative mocks for the test class (mock 
fields)


Declarative mocks for test methods
 (parameters, local fields)






Special fields for "any" argument matching





Use of an special field to specify invocation
 results






Use of special fields to specify invocation
 count constraints






Expectations with custom error messages





On-demand mocking of unspecified implementation classes





Capture of instances created by code under
 test






Recording & verification of expectations in
 loops






Support for covariant return types





"Duck typing" mocks for state-based tests





Single jar file in the classpath is sufficient to
 use mocking API



N/AN/A
Total6/327/3213/3111/319/3114/3032/32
Total when ignoring JMockit-only features6/227/2213/2111/219/2114/2022/22

Refer Url:

===============================


EasyMock

官方主页 http://www.easymock.org/

最新版本 3.1(2011-11-08)

EasyMock 使用方法与原理剖析 https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/(07年的文档,使用的是V2.3)

 

jMock

官方主页 http://jmock.org/

最新版本 2.6.0(2012-12-19) 

 

mockito

官方主页 http://code.google.com/p/mockito/

最新版本 1.9.5(2012-10-06)

Java mocking is dominated by expect-run-verify libraries like EasyMock or jMock. Mockito offers simpler and more intuitive approach: you ask questions about interactions after execution. Using mockito, you can verify what you want. Using expect-run-verify libraries you are often forced to look after irrelevant interactions.

 

No expect-run-verify also means that Mockito mocks are often ready without expensive setup upfront. They aim to be transparent and let the developer to focus on testing selected behavior rather than absorb attention.

 

Mockito has very slim API, almost no time is needed to start mocking. There is only one kind of mock, there is only one way of creating mocks. Just remember that stubbing goes before execution, verifications of interactions go afterwards. You'll soon notice how natural is that kind of mocking when TDD-ing java code.

 

Mockito has similar syntax to EasyMock, therefore you can refactor safely. Mockito doesn't understand the notion of 'expectation'. There is only stubbing and verifications.

 

Mockito VS EasyMock http://code.google.com/p/mockito/wiki/MockitoVSEasyMock

 

jmockit

官方主页 http://code.google.com/p/jmockit/

最新版本 1.2(2013-04-21)

JMockit allows developers to write unit and integration tests without the testability issues typically found with other mocking APIs. Tests can easily be written that will mock final classes, static methods, constructors, and so on. There are no limitations.

 

Most importantly, with JMockit all programming practices, patterns, and styles are supported equally, without forcing developers to make compromises just for the sake of unit testing.

 

PowerMock

官方主页 http://code.google.com/p/powermock/

最新版本 1.5(2012-12-04)

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. By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with the supported mock frameworks will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock aims to extend the existing API's with a small number of methods and annotations to enable the extra features. Currently PowerMock supports EasyMock and Mockito.

PowerMock简介 http://www.ibm.com/developerworks/cn/java/j-lo-powermock/

 

其他相关资料

Unitils 

官方主页 http://www.unitils.org

最新版本 3.3(2011-12-22)

    Unitils构建在DBUnit与EasyMock项目之上并与JUnit和TestNG相结合。支持数据库测试,支持利用mock对象进行测试并提供与Spring和Hibernate相集成。Unitils设计成以一种高度可配置和松散偶合的方式来添加这些服务到单元测试中。


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
单元测试-如何去mock对象
有效使用Mock编写java单元测试
Powermock2.0.0 详细 总结
玩花招的PowerMock
微服务实战之Mock
收藏!Java编程技巧之单元测试用例编写流程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服