zoukankan      html  css  js  c++  java
  • android开发AndroidJUnit4单元测试模板编写,以及如何在单元测试代码里进行权限申请

    
    1. build.gradle文件添加依赖
    android {
        defaultConfig {
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        testImplementation 'junit:junit:4.+'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
        androidTestImplementation "org.mockito:mockito-core:2.8.9"
        androidTestImplementation "org.mockito:mockito-android:2.8.9"
        androidTestImplementation 'com.android.support.test:rules:1.0.2'
    }
    
    
    
    2. AndroidManifest文件添加权限
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.xx.xxx" >
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </manifest>
    
    
    
    3. 通过GrantPermissionRule申请权限
    @RunWith(AndroidJUnit4.class)
    public class EmptyTest {
    
        @Rule
        public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
            	Manifest.permission.INTERNET);
    
        @Test
        public void testEmpty() {
            Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();  //获取Context上下文
            String text = "";
            Assert.assertTrue(TextUtils.isEmpty(text));
        }
    }
    
    
  • 相关阅读:
    rsync特性
    01 什么是爬虫
    celery的使用
    redis的使用
    GIT使用大全
    多项式的高级运算
    SP1557 GSS2
    题解 CF997E 【Good Subsegments】
    P3920 [WC2014]紫荆花之恋
    题解 P3750 【[六省联考2017]分手是祝愿】
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/15479362.html
Copyright © 2011-2022 走看看