AndroidStudio/Gradle with powermock

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

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.

回答1:

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

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' 


回答2:

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' 


回答3:

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' 


回答4:

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.



回答5:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!