Java 各種Mock工具比較
轉自:
可用的Mock Toolkit有許多,比較常見的有EasyMock, Jmock和JMockit等等,到底選哪個呢,Jmockit的官網上有個特性對比列表,很詳細:
Refer Url:===============================
EasyMock
最新版本 3.1(2011-11-08)
jMock
最新版本 2.6.0(2012-12-19)
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.
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
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
最新版本 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.
其他相關資料
Unitils
最新版本 3.3(2011-12-22)
Unitils構建在DBUnit與EasyMock專案之上並與JUnit和TestNG相結合。支援資料庫測試,支援利用mock物件進行測試並提供與Spring和Hibernate相整合。Unitils設計成以一種高度可配置和鬆散偶合的方式來新增這些服務到單元測試中。