Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VerifyError with PowerMock on Android

I'm trying to use PowerMock to mock some classes used in testing our Android app (BluetoothSocket for example).

I have downloaded the zip file on PowerMock's google code page with all dependencies and added them to my Android test project (including build path).

However when I try to use PowerMock like this:

@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 only get this error when running the test project as an Android JUnit Test, if I run it as a regular JUnit test powermock works, but then all my tests that needs the Android framework naturally fails.

How can I solve this? I'm very new to unit testing so I don't really understand all concepts yet.

like image 361
monoceres Avatar asked Jul 03 '12 14:07

monoceres


1 Answers

You might have ambiguous versions of JUnit libraries in the classpath setup of your tests under Android. Are you trying to run with JUnit 3 or 4? Your code is using 4's annotations, but also extending 3's TestCase base class, and you cannot mix the two. It's best nowadays to use JUnit 4. I'm not too familiar with Android development, so check what version of JUnit the Android environment is looking for. The next thing to check is if you're using the right version of PowerMock, as there are different versions of it for JUnit 3 and 4.

like image 162
Oliver Hernandez Avatar answered Oct 29 '22 08:10

Oliver Hernandez