2013年3月30日 星期六

JMock - java.lang.IllegalArgumentException: ... is not an interface


解法:


出現:java.lang.IllegalArgumentException: unittest.MathM is not an interface
at java.lang.reflect.Proxy.getProxyClass(Unknown Source)

....


(unittest.MathM為自訂預測試Class)


Mockery _context = new JUnit4Mockery();

改為


 
 Mockery _context = new JUnit4Mockery(){{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};



探討:

看到exception上其實說得很明白 unittest.MathM is not an interface,就大概可以猜到若要將我們欲使用的Mock物件(這裡為MathM)變成可使用的,就得為interface;

以下是Mockery.setImposteriser()的doc:


Changes the imposteriser used to adapt mock objects to the mocked type. The default imposteriser allows a test to mock interfaces but not classes, so you'll have to plug a different imposteriser into the Mockery if you want to mock classes.



因此對於已經為concreate class的 object來說,就得另外使用  setImposteriser(ClassImposteriser.INSTANCE); 改變其中的Imposteriser才能做到。

原本對於細節實作還挺有興趣的,但無奈功力不夠,追了一些source就追不下去了0rz...只能大約知道有使用reflection機制去實作。
看來真的要開始培養自己鑽研open source的能力了...................


沒有留言:

張貼留言