51

I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions.

Could anybody show a correct way to do it?

Thanks.

midnight
  • 3,420
  • 3
  • 36
  • 58

7 Answers7

65

I'm posting in order to help future readers, you need to add these dependencies for powermock in AS

testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-api-mockito:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4:1.6.2'
Community
  • 1
  • 1
Bhargav
  • 8,118
  • 6
  • 40
  • 63
25

Add the following lines to your dependencies{} block:

testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.6.5'

And if you would like to use PowerMockito, add the following line:

testCompile 'org.powermock:powermock-api-mockito:1.6.5'
plátano plomo
  • 1,672
  • 1
  • 18
  • 26
5

In the build script, add the following:

sourceSets {
    unitTest {
        java.srcDir file('*your test directory*') //for example: tests/java
    }
}

android {
    sourceSets {
        instrumentTest.setRoot('*your root test directory*') //for example: tests
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.powermock:powermock-mockito-release-full:1.4.9'
}

Then, do gradle unitTest from the command line.

Hope that works. If it doesn't, post the output of the command line.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
sasfour
  • 399
  • 1
  • 3
  • 10
  • 2
    Is this answer is intended to run unit tests on the workstation as opposed to the phone or tablet? And that would also require explicitly mocking any Android classes used to test only the logic you have written yourself? (and no UI tests) (or perhaps use RoboElectric to provide jdk implementations of the Android classes?) – Stan Kurdziel Mar 04 '15 at 23:52
  • 2
    I had to add `testCompile 'junit:junit:4.12'
    testCompile 'org.powermock:powermock-api-mockito:1.6.1'
    testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1'
    testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'
    testCompile 'org.powermock:powermock-module-junit4:1.6.1'`
    to get powermock import to work in Android Studio. Got the hint here: https://code.google.com/p/android/issues/detail?id=147472. - **Sorry for the bad formatting, I can't seem to get line breaks in comments?**
    – jokki Apr 21 '15 at 08:25
  • 1
    @jokki make this a separate answer! – Boy Oct 12 '15 at 09:56
5

If you want to use more recent versions of Mockito, you can use something like this, which is adapted from the mockito 2 Powermock docs. Do make sure you use the right version of PowerMock for the given version of Mockito.

...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.4.0"
testCompile 'org.powermock:powermock-module-junit4:1.7.0RC2',
            'org.powermock:powermock-api-mockito2:1.7.0RC2'
m01
  • 9,033
  • 6
  • 32
  • 58
  • 2
    See https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 and https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 for the latest versions – Rajath Oct 03 '17 at 02:20
3
// mockito
testImplementation 'org.mockito:mockito-core:2.4.0'
androidTestImplementation 'org.mockito:mockito-core:2.4.0'
// PowerMock
testImplementation 'org.powermock:powermock-core:1.7.0RC2'
testImplementation 'org.powermock:powermock-module-junit4:1.7.0RC2'
testImplementation 'org.powermock:powermock-api-mockito2:1.7.0RC2'
J.zhang
  • 31
  • 1
  • Please consider giving more elaboration of your codes, and also check [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). – ytu May 30 '18 at 02:22
  • ok,Add the following lines to your dependencies{} block,if you would like to use PowerMockito with Mockito.(https://github.com/powermock/powermock/wiki/Mockito) – J.zhang Jun 03 '18 at 12:50
1

I have used same as @Bhargav used with some additional features added with it

  • code coverage for test case (if testCoverageEnabled is true, then it enable Jacoco tool)
  • unit test will test only your code and do not depend on any particular behaviour of Android platform by using (UnitTests.returnDefaultValues = true)

Add this marked lines in build.gradle to enable JUnit, PowerMockito, JaCoCo

enter image description here enter image description here

anand krish
  • 4,281
  • 4
  • 44
  • 47
1

My example compiled from all the other answers I could find, with latest versions at the time of writing:

app\build.gradle

dependencies {
    testImplementation group: 'junit',         name: 'junit',                   version: '4.13'
    ...
    testImplementation group: 'org.powermock', name: 'powermock-api-mockito2',  version: '2.0.7'
    testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
}

A test class where say Android Log class was static mocked.

import android.util.Log;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Log.class})
public class DateUtilsTest {
    @BeforeClass
    public static void beforeClass() {
        PowerMockito.mockStatic(Log.class);
    }
    ...
}
Dan Dragut
  • 41
  • 1
  • 5