Java 各种Mock工具比较 – yasi

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

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

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

FeatureEasyMockjMockMockitoUnitils MockPowerMock:EasyMockAPIPowerMock:MockitoAPIJMockitInvocation count constraints√√√√√√Recording strict expectations√√√√Explicit verification√√√√Partial mocking√√√√√√No method call to switch from record to replay√√√√No extra code for implicit verificationN/AN/AN/A√No extra "prepare for test" code√√√√√No need to use@RunWithannotation or basetest class√√√√Consistent syntax betweenvoidand non-voidmethods√√√Argument matchersfor some parameters only,not all√√Easier argument matching based on propertiesof value objects√√√√√√Cascading mocks√√√√Support for mocking multiple interfaces√√√Support for mocking annotation types√√√√√Partially ordered expectations√√Mocking of constructors andfinal/static/native/privatemethods√√√Declarative application of mocks/stubs towhole test classes√√√Auto-injection of mocks√√√√Mocking of "new-ed" objects√√√Support for mocking enum types√√√Declarative mocks for the test class (mockfields)√√√√√Declarative mocks for test methods(parameters, local fields)√Special fields for "any" argument matching√Use of an special field to specify invocationresults√Use of special fields to specify invocationcount constraints√Expectations with custom error messages√On-demand mocking of unspecified implementation classes√Capture of instances created by code undertest√Recording & verification of expectations inloops√Support for covariant return types√"Duck typing" mocks for state-based tests√Single jar file in the classpath is sufficient touse mocking API√N/AN/A√Total6/327/3213/3111/319/3114/3032/32Total when ignoring JMockit-only features6/227/2213/2111/219/2114/2022/22

Refer Url:http://code.google.com/p/jmockit/wiki/MockingToolkitComparisonMatrix

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

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 EasyMockhttp://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 mockfinalclasses,staticmethods, 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设计成以一种高度可配置和松散偶合的方式来添加这些服务到单元测试中。

可是我要如何在浅薄的纸上为你画上我所有的命轮?

Java 各种Mock工具比较 – yasi

相关文章:

你感兴趣的文章:

标签云: