带有 powermock 的 AndroidStudio/Gradle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25136862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AndroidStudio/Gradle with powermock
提问by midnight
I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions.
我找不到有关如何使用 Android Studio/Gradle 设置 powermock 的任何信息。我尝试过的一切都会导致构建异常。
Could anybody show a correct way to do it?
有人可以展示正确的方法吗?
Thanks.
谢谢。
回答by Bhargav
I'm posting in order to help future readers, you need to add these dependencies for powermock in AS
我发帖是为了帮助未来的读者,您需要在 AS 中为 powermock 添加这些依赖项
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'
回答by plátano plomo
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:
如果您想使用 PowerMockito,请添加以下行:
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
回答by m01
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.
如果你想使用更新版本的 Mockito,你可以使用类似这样的东西,它改编自mockito 2 Powermock 文档。请确保为给定版本的 Mockito使用正确版本的 PowerMock。
...
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'
回答by sasfour
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.
然后,从命令行执行 gradle unitTest。
Hope that works. If it doesn't, post the output of the command line.
希望有效。如果没有,请发布命令行的输出。
回答by J.zhang
// 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'
回答by Dan Dragut
My example compiled from all the other answers I could find, with latest versions at the time of writing:
我的示例是根据我能找到的所有其他答案编译的,在撰写本文时使用最新版本:
app\build.gradle
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.
一个测试类,其中说 AndroidLog
类是静态模拟的。
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);
}
...
}
回答by anand krish
I have used same as @Bhargav used with some additional features added with it
我使用了与@Bhargav 相同的用法,并添加了一些附加功能
- code coverage for test case (if testCoverageEnabledis true, then it enable Jacocotool)
- unit test will test only your code and do not depend on any particular behaviour of Android platform by using (UnitTests.returnDefaultValues = true)
- 测试用例的代码覆盖率(如果testCoverageEnabled为true,则启用Jacoco工具)
- 单元测试将仅测试您的代码,不依赖于 Android 平台的任何特定行为(UnitTests.returnDefaultValues = true)
Add this marked lines in build.gradleto enable JUnit, PowerMockito, JaCoCo
在build.gradle 中添加此标记行以启用JUnit, PowerMockito, JaCoCo