Development issue/problem:
I couldn’t find any information on how to configure the powermock with Android Studio/Gradle. Everything I’ve tried has resulted in creating exceptions.
Can someone point me in the right direction?
Thank you so much!
How can I solve this problem?
Solution 1:
I publish here to help future readers, you have to add these dependencies for the layout in AS
testImplementation ‘juniper:juniper4.12’testImplementation ‘org.powder:powder-api-moquito:1.6.2’testImplementation ‘org.powder: powder-module-api4 rule-agent: 1.6.2’testImplementation ‘org.powder: powder-module-unit4 rule: 1.6.2’testImplementation ‘org.powder: powder-module-unit4:1.6.2’testImplementation ‘org.powder: powder-module-unit4:1.6.2’testImplementation ‘org.powder: powder-module-unit4:1.6.2
Solution 2:
Add the following lines to your dependency 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 want to use PowerMockito, add the following line:
testCompile ‘org.powermock: powermock-api-mockito: 1.6.5
Solution 3:
Add the following in the build script:
sources {
unitTest {
java.srcDir file (‘*your test directory*’) // example: tests/java
}
}
android {
sourceSets {
instrumentTest.setRoot(‘*your root directory*’) // example: tests
}
}
Deposits {
mavenCentral()
}
Dependencies {
testCompile junit:junit:4.11
testCompile org.powermock:powermockito-release-full:1.4.9
}
Then perform a test of the Gradle unit from the command line.
I hope it works. Otherwise, display the command line output.
Solution 4:
If you want to use a more recent version of Mockito, you can use something similar to the Mockito 2 Powermock dock. Make sure you use the correct version of PowerMock for this version of Mockito.
…testCompile ‘junit:junit:4.12’testCompile org.mockito:mockito-core:2.4.0testCompile ‘org.powermock:powermock-module-junit4:1.7.0RC2’, ‘org.powermock:powermock-api-mockito2:1.7.0RC2’, ‘org.powermock:powermock-api-mockito2:1.7.0RC2’…
Solution No 5:
// mockitotestImplementation ‘org.mockito:mockito-core:2.4.0’androidTestImplementation ‘org.mockito:mockito-core:2.4.0’// PowerMocktestImplementation ‘org.mockito:mockito-core:2.4.0’// PowerMocktestImplementationpowder: powder-core: 1.7.0RC2testImplementation ‘org.powder: powder module unit4: 1.7.0RC2’ testImplementation ‘org.powder: powder-api-mockito2: 1.7.0RC2
Solution No 6:
I used the same as @Bhargav with some extra features added.
- Code coverage for the test case (if testCoverageEnabled is correct, activate the Jacoco tool).
- UnitTests.return Test only your code and does not depend on any particular behaviour of the Android platform when used (UnitTests.returnDefaultValues = true).
Add this checked line to build.gradle to enable JUnit, PowerMockito, JaCoCo.
Solution No 7:
My example is made up of all the other answers I could find, with the latest versions at the time of writing:
appbuild.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: powermock-module-junit4 2.0.7
}
The test class where they say the log android class was static bullying.
import android.util.log;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner RunWith;
import org.runner.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner ;
@RunMet(PowerMockRunner.class)
@PrepareForTest({Log.class})
public class DateUtilsTest {
@BeforeClass
public static blank forClass() {
PowerMockito.mockStatic(Log.class);
}
.
}
Good luck!
Related Tags:
powermock-module-junit4 gradle,powermockito dependency gradle,spring boot powermock gradle dependency,powermock-api-mockito gradle,powermock gradle junit 5,powermockito spy,powermockito android tutorial,powermock android