I am trying to setup power mokito on my android project, to test static methods
I wrote these dependencies
provided 'junit:junit:4.11'
androidTestCompile ('junit:junit:4.11'){
exclude module:"hamcrest-core"
exclude module: "assertj-core"
}
// provided 'org.mockito:mockito-core:2.0.3-beta'
// androidTestCompile ('org.mockito:mockito-core:2.0.3-beta'){
// exclude module:"hamcrest-core"
// exclude module:"assertj-core"
// }
provided 'org.powermock:powermock-mockito-release-full:1.4.9'
androidTestCompile ('org.powermock:powermock-mockito-release-full:1.4.9'){
exclude module : "hamcrest-core"
// exclude module : "mockito-all"
exclude module: "assertj-core"
exclude module: "objenesis"
}
Then i wrote my test
import android.test.AndroidTestCase;
import com.myapp.IntentActions;
import org.junit.Test;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
public class IntentActionsTest extends AndroidTestCase {
@Test
public void testIntentFilter() {
//IntentActions class is a factory
//for intent filters
//I want to make sure that the filter is set up correctly
//This is why I want to use PowerMockito
mockStatic(IntentActions.class);
}
}
The problem is when I run this test I get this error
java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:68)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:50)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:100)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
at com.myapp.tests.IntentActionsTest.testIntentFilter(IntentActionsTest.java:22)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Finally I found the problem, you are trying to run the PowerMockito test on Dalvik VM (Android Instrumentation Test). It is not possible, you should use the JVM to run the PowerMockito tests (Android Studio Unit Tests).
With the release 1.1 of the Android Studio was introduced the support for unit tests that can run on JVM.
So, to fix this issue you need to follow this tutorial (https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support) to enable the unit tests to run on the JVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With