我試圖用PowerMock來模擬一些用於測試我們的Android應用程序的類(例如BluetoothSocket)。Android上的PowerMock驗證錯誤
我已經將PowerMock的google代碼頁上的zip文件下載到所有的依賴關係中,並將它們添加到我的Android測試項目(包括構建路徑)中。
然而,當我嘗試使用PowerMock是這樣的:
@RunWith(PowerMockRunner.class)
@PrepareForTest(NetworkUtil.class)
public class TestSendAck extends TestCase{
@Test
public void testGenerateURL() {
PowerMock.mockStatic(NetworkUtil.class);
EasyMock.expect(NetworkUtil.getLocalHostname()).andReturn("triumph");
PowerMock.replayAll();
PowerMock.verifyAll();
}
}
我碰到下面的堆棧跟蹤:
java.lang.ExceptionInInitializerError
at org.easymock.internal.ClassProxyFactory.createEnhancer(ClassProxyFactory.java:249)
at org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:159)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:59)
at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163)
at org.powermock.api.easymock.PowerMock.mockStatic(PowerMock.java:287)
at se.metrima.mafield.test.TestSendAck.testGenerateURL(TestSendAck.java:19)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Caused by: java.lang.VerifyError: net.sf.cglib.core.ReflectUtils
at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:166)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
... 19 more
運行測試項目作爲一個Android JUnit測試時,我只得到這個錯誤,如果我將它作爲常規JUnit測試powermock運行,但那麼所有需要Android框架的測試自然會失敗。
我該如何解決這個問題?我對單元測試非常陌生,所以我還沒有真正理解所有的概念。
你解決了嗎? –