简体   繁体   English

带有 powermock 的 AndroidStudio/Gradle

[英]AndroidStudio/Gradle with powermock

I couldn't find any info on how to setup powermock with Android Studio/Gradle.我找不到有关如何使用 Android Studio/Gradle 设置 powermock 的任何信息。 Everything I've tried resulted in build exceptions.我尝试过的一切都会导致构建异常。

Could anybody show a correct way to do it?有人可以展示正确的方法吗?

Thanks.谢谢。

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'

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'

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.如果没有,请发布命令行的输出。

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 .如果你想使用更新版本的 Mockito,你可以使用类似这样的东西,它改编自mockito 2 Powermock 文档 Do make sure you use the right version of PowerMock for the given version of Mockito .请确保为给定版本的 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'
// 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'

I have used same as @Bhargav used with some additional features added with it我使用了与@Bhargav 相同的用法,并添加了一些附加功能

  • code coverage for test case (if testCoverageEnabled is true , then it enable Jacoco tool)测试用例的代码覆盖率(如果testCoverageEnabledtrue ,则启用Jacoco工具)
  • unit test will test only your code and do not depend on any particular behaviour of Android platform by using ( UnitTests.returnDefaultValues = true )单元测试将仅测试您的代码,不依赖于 Android 平台的任何特定行为( UnitTests.returnDefaultValues = true

Add this marked lines in build.gradle to enable JUnit, PowerMockito, JaCoCobuild.gradle 中添加此标记行以启用JUnit, PowerMockito, JaCoCo

在此处输入图片说明 在此处输入图片说明

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.一个测试类,其中说 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);
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM