VerifyError with PowerMock on Android

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.

+6
source share
1 answer

You may have ambiguous versions of the JUnit libraries in the class settings for your Android tests. Are you trying to run JUnit 3 or 4? Your code uses 4 annotations, but also extends the 3 base class of TestCase, and you cannot mix them. It is best to use JUnit 4. I am not very familiar with Android development, so check which version of JUnit for the Android environment is looking for. The next thing to check is that you are using the correct version of PowerMock, since there are different versions for JUnit 3 and 4.

+2
source

Source: https://habr.com/ru/post/919596/


All Articles