修改gradle.properties文件,加入下面2行:
android.useAndroidX=true android.enableJetifier=true
修改app的build.gradle文件:
defaultConfig中加入
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dependencies中加入
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
建立一个测试基类如下:
@RunWith(AndroidJUnit4.class)
public class BaseTest {
protected Context context;
@Before
public void before() {
context = InstrumentationRegistry
.getInstrumentation()
.getTargetContext();
}
}
测试类继承自该基类即可。
参考:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END













