分享

Java 各种Mock工具比较

 instl 2015-03-19

转自:

http://billben./blog/1872196

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


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

Feature EasyMock jMock Mockito Unitils Mock PowerMock:
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/A N/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/A N/A
Total 6/32 7/32 13/31 11/31 9/31 14/30 32/32
Total when ignoring JMockit-only features 6/22 7/22 13/21 11/21 9/21 14/20 22/22

Refer Url:

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


EasyMock

官方主页 http://www./

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

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

 

jMock

官方主页 http:///

最新版本 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.

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

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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多