AndroidStudio/Gradle with powermock

后端 未结 7 1418
小鲜肉
小鲜肉 2020-12-29 01:28

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

相关标签:
7条回答
  • 2020-12-29 01:40
    // 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'
    
    0 讨论(0)
  • 2020-12-29 01:43

    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.

    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);
        }
        ...
    }
    
    0 讨论(0)
  • 2020-12-29 01:45

    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'
    
    0 讨论(0)
  • 2020-12-29 01:46

    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'
    
    0 讨论(0)
  • 2020-12-29 01:51

    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.

    0 讨论(0)
  • 2020-12-29 01:56

    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

    0 讨论(0)
提交回复
热议问题