AndroidStudio/Gradle with powermock

后端 未结 7 1516
小鲜肉
小鲜肉 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: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);
        }
        ...
    }
    

提交回复
热议问题