jmockit

JMockit 是用以幫助開發人員編寫測試程式的一組工具和API,該項目完全基於 Java 5 SE 的 java.lang.instrument 包開發,內部使用 ASM 庫來修改Java的Bytecode。

基本介紹

名稱,簡介,示例,

名稱

jmockit全稱Mocking with Jmockit

簡介

單元測試中,經常需要進行一些mock操作。已經有了一些比較不錯的框架在做這些事情,比如:EasyMock,他在大多數情況下運行良好,但是對於某些結構的設計卻顯得無能為力。
EasyMock等眾多的mock框架僅能mock一些public,non static or final的方法,在大多數情況下這並沒有什麼問題,他可以處理大多數的問題,但是當測試的代碼包含了一些靜態方法,可能就讓問題變得難以解決,有一種選擇即是重構他(過多的靜態方法可能預示著這並不是一個很好的設計),但是當你使用外部引用庫所提供的方法,問題又該如何解決呢?
JMockit是一個能幫我們解決以上問題的輕量級框架,他允許你動態的改變已有的方法,這主要基於java 1.5的Instrumentation框架,這樣便可以使得JMockit能夠適應幾乎所有的設計。他允許你重定義private,static and final方法,甚至是no-arg constructors都能夠並輕易的重定義。
在實際的項目中有些方法可以重定義而有些不行,為了更好的說明如何對方法進行重定義,下面有一個簡單的類和對應測試代碼的demo,他儘可能考慮到了幾乎所有的情況,供大家方便的學習。

示例

ClassToMock :
publicclassClassToMock
privateString memberToSet;
privatestaticString staticMember;
static
staticMember= "Static initialized";
publicClassToMock
this.memberToSet= "Member set by original constructor";
publicClassToMock(String value)
this.memberToSet= "Member set by original constructor";
publicString publicMethod
return"Original public method";
protectedString protectedMethod
return"Original protected method";
String defaultMethod
return"Original default method";
publicString methodThatUsesPrivateMethod
returnprivateMethod;
privateString privateMethod
return"Original private method";
publicString getMemberToSet
returnmemberToSet;
publicString getStaticMember
returnstaticMember。
ClassToMockTest :
importjunit.framework.TestCase;
importmockit.Mockit;
publicclassClassToMockTest extendsTestCase
privateClassToMock mockedClass;
publicstaticclassReplacement
static
publicReplacement
publicReplacement(String test)
publicString publicMethod
return"Replaced public method";
publicString protectedMethod
return"Replaced protected method";
publicString defaultMethod
return"Replaced default method";
publicString privateMethod
return"Replaced private method";
protectedvoidsetUp throwsException
Mockit.redefineMethods(ClassToMock.class, Replacement.class);
mockedClass = newClassToMock("test");
protectedvoidtearDown throwsException
Mockit.restoreAllOriginalDefinitions;
Public methods can be replaced
publicvoidtestReplacePublic
assertEquals("Replaced public method", mockedClass.publicMethod);
Protected methods can be replaced.
The replacement method should be declared public however
publicvoidtestReplaceProtected
assertEquals("Replaced protected method", mockedClass.protectedMethod);
Package accessable methods can be replaced
The replacement method should be declared public however
publicvoidtestReplaceDefault
assertEquals("Replaced default method", mockedClass.defaultMethod);
Private methods can be replaced
The replacement method should be declared public however
publicvoidtestReplacePrivate
assertEquals("Replaced private method", mockedClass
.methodThatUsesPrivateMethod);
Non-default constructors can be replaced
Your mock definition must have a default constructor however
publicvoidtestReplaceConstructor
assertEquals(null, mockedClass.getMemberToSet);
Default constructors <b>can't</b> be replaced
publicvoidtestReplaceDefaultConstructor
mockedClass = newClassToMock;
assertEquals("Member set by original constructor", mockedClass
.getMemberToSet);
Static initializers <b>can't</b> be replaced
publicvoidtestReplaceStaticBlock
assertEquals("Static initialized", mockedClass.getStaticMember);

相關詞條

熱門詞條

聯絡我們