9

I am using Android Studio 1.2 and the com.android.tools.build:gradle:1.2.2 plugin.

Attempt 1

I include the following in my app/build.gradle:

androidTestCompile ('com.google.dexmaker:dexmaker-mockito:1.2')
androidTestCompile ('org.powermock:powermock-mockito-release-full:1.6.2')

but then the PowerMockito package in not available for import:

error: cannot find symbol
    PowerMockito.mockStatic(DatastoreFactory.class);
    ^

Attempt 2

I include the following in my app/build.gradle:

androidTestCompile ('org.powermock:powermock-api-mockito:1.6.2') {
    exclude module: 'hamcrest-core'
    exclude module: 'objenesis'
}

androidTestCompile ('org.powermock:powermock-module-junit4:1.6.2') {
    exclude module: 'hamcrest-core'
    exclude module: 'objenesis'
}

which is a trial-and-error offshoot of this q/a here: AndroidStudio/Gradle with powermock

This compiles but when run Mockito gives a runtime 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.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49)
    at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24)
    at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.mock(Mockito.java:1285)
    at org.mockito.Mockito.mock(Mockito.java:1163)
    at com.mdsol.naga.FormPusherTest.setUp(FormPusherTest.java:40)
    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)

Is anyone using Powermock successfully with Android Studio 1.2? Please share your build.gradle - thanks!

Community
  • 1
  • 1
jtomson
  • 456
  • 5
  • 10

1 Answers1

14

It should be good already. At least in my case its working fine.

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:2.0.5-beta'
    testCompile 'com.android.support:support-v4:22.0.0'
    testCompile ('org.powermock:powermock-api-mockito:1.6.2') {
        exclude module: 'hamcrest-core'
        exclude module: 'objenesis'
    }
    testCompile ('org.powermock:powermock-module-junit4:1.6.2') {
        exclude module: 'hamcrest-core'
        exclude module: 'objenesis'
    }
}

Also, remember to select Unit Tests in the Test Artifact section of Build Variants in Android Studio. You need to make sure you are using the right project structure, Gradle version and testCompile.

You can find the article about this here: http://vexdev.com/2015/05/06/unit-testing-android/

And the whole project: https://github.com/vexdev/AndroidUnitTest

Luca Vitucci
  • 3,674
  • 4
  • 36
  • 60
  • 1
    For some reason I need to use `androidTestCompile` instead of `testCompile` or I receive: `Error:(135, 0) Gradle DSL method not found: 'testCompile()'` – jtomson May 07 '15 at 14:33
  • I think I see - are you using Robolectric to run on the JVM as per this answer? http://stackoverflow.com/a/29024318/204209 – jtomson May 07 '15 at 14:36
  • 1
    Nope, I'm not using Robolectric at all. You need to make sure that on Android Studio you are performing Unit Test (Check on build variants on the left, select build artifact: Unit Tests). Check my example project on GitHub. – Luca Vitucci May 07 '15 at 15:21
  • @jtomson have you enabled unit testing in the Android Studio experimental settings? – Sam Dozor May 07 '15 at 15:26
  • @SamDozor , This settings is no more experimental as of A.S. 1.2.0, but he needs to switch to Unit Test. – Luca Vitucci May 07 '15 at 15:31
  • 1
    Ah I see - I was using the old gradle plugin. Switching to `'com.android.tools.build:gradle:1.1.0'` and using these dependencies did the trick! Thanks – jtomson May 07 '15 at 17:44
  • I still cannot use `@RunWith(PowerMockRunner.class)`. – IgorGanapolsky Oct 19 '16 at 17:52