AndroidStudio/Gradle与PowerMock的使用

51

我找不到有关如何在Android Studio/Gradle中设置PowerMock的信息。我尝试了一切,但都导致构建异常。

能否有人展示正确的做法?

谢谢。

7个回答

65

我发帖是为了帮助未来的读者,你需要在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'

25

将以下代码添加到您的dependencies {}块中:

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

如果您想使用PowerMockito,请添加以下行:

testCompile 'org.powermock:powermock-api-mockito:1.6.5'

5

2
请查看 https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 和 https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 获取最新版本。 - Rajath

5
在构建脚本中添加以下内容:
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'
}

接下来,请从命令行执行gradle unitTest。

希望这能起作用。如果不行,请发布命令行的输出。


2
这个答案是否旨在在工作站上运行单元测试,而不是在手机或平板电脑上运行?这也需要显式地模拟任何用于测试您自己编写的逻辑的Android类吗?(没有UI测试)(或者使用RoboElectric提供Android类的jdk实现?) - Stan Kurdziel
2
我不得不添加以下内容才能使Android Studio中的powermock导入正常工作:<br/> testCompile 'junit:junit:4.12'<br/> testCompile 'org.powermock:powermock-api-mockito:1.6.1'<br/> testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1'<br/> testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'<br/> testCompile 'org.powermock:powermock-module-junit4:1.6.1'<br/> 在这里得到了提示:https://code.google.com/p/android/issues/detail?id=147472。- 抱歉格式不好,我无法在评论中换行? - jokki
1
@jokki请将此作为单独的答案! - Boy

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'

请考虑更详细地解释您的代码,并查看如何撰写好答案? - ytu
如果您想要在Mockito中使用PowerMockito,可以将以下代码添加到您的dependencies{}块中。(https://github.com/powermock/powermock/wiki/Mockito) - J.zhang

1

我使用了与@Bhargav相同的内容,并添加了一些附加功能

  • 测试用例的代码覆盖率(如果testCoverageEnabledtrue,则启用Jacoco工具)
  • 使用(UnitTests.returnDefaultValues = true)来测试只针对您的代码进行单元测试,不依赖于Android平台的任何特定行为

build.gradle中添加这些标记行以启用JUnit,PowerMockito,JaCoCo

enter image description here enter image description here


1

以下是我从所有可找到的答案中编译的示例,使用的是最新版本:

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

一个测试类,在这个类中,Android的Log类被静态模拟了。
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);
    }
    ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接