I am trying to use PowerMock to bully some of the classes used to test our Android application (e.g. BluetoothSocket).
I downloaded the zip file on the Google PowerMock code page with all the dependencies and added them to my Android test project (including the build path).
However, when I try to use PowerMock as follows:
@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(); } }
I get the following stack trace:
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
I get this error only when starting a test project as a JUnit test for Android, if I run it as a regular JUnit test powermock simulator, but then all my tests that need the Android framework naturally fail.
How can i solve this? I am very new to unit testing, so I donβt understand all the concepts yet.
source share