AndroidStudio/Gradle with powermock

AndroidGradlePowermock

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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'

Solution 2 - Android

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'

Solution 3 - Android

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.

Solution 4 - Android

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'

Solution 5 - Android

// 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'

Solution 6 - Android

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

Solution 7 - Android

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);
	}
    ...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmidnightView Question on Stackoverflow
Solution 1 - AndroidBhargavView Answer on Stackoverflow
Solution 2 - Androidplátano plomoView Answer on Stackoverflow
Solution 3 - AndroidsasfourView Answer on Stackoverflow
Solution 4 - Androidm01View Answer on Stackoverflow
Solution 5 - AndroidJ.zhangView Answer on Stackoverflow
Solution 6 - Androidanand krishView Answer on Stackoverflow
Solution 7 - AndroidDan DragutView Answer on Stackoverflow