ApplicationTestCase在API级别24中已弃用

我使用API 24Android Studio 2.1.2上创build了一个默认的空项目。 在示例项目中,Google提供了一个折旧类ApplicationTestCase

此类已在API级别24中弃用。请改用ActivityTestRule。 应该使用Androidtesting支持库编写新的testing。

样品:

import android.app.Application; import android.test.ApplicationTestCase; /** * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> */ public class ApplicationTest extends ApplicationTestCase<Application> { public ApplicationTest() { super(Application.class); } } 

我的问题:为什么Androidtesting案例已被弃用? 如何用ActivityTestRulereplaceApplicationTestCase?


编辑:

我尝试与Expresso ,但在API 24compileSdkVersion 24 )我有这个错误:

 Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. Error:Conflict with dependency 'com.android.support:design'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. Error:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 

当我尝试添加这个库在我的build.gradle:

 // Android JUnit Runner androidTestCompile 'com.android.support.test:runner:0.5' // JUnit4 Rules androidTestCompile 'com.android.support.test:rules:0.5' // Espresso core androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' // Espresso-web for WebView support androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2' // Espresso-idling-resource for synchronization with background jobs androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2' 

我的结论是,目前Androidtesting用例Expresso都不能在Android API 24上运行 。 这是正确的吗?


编辑:2016-08-05

我修复了以前的Expresso错误:

 def espressoVersion = '2.2.2' def testRunnerVersion = '0.5' androidTestCompile "com.android.support.test:rules:${testRunnerVersion}" androidTestCompile "com.android.support.test.espresso:espresso-core:${espressoVersion}" configurations.androidTestCompile.dependencies.each { androidTestCompileDependency -> androidTestCompileDependency.exclude group: 'com.android.support' } 

Solutions Collecting From Web of "ApplicationTestCase在API级别24中已弃用"

Android Studio 2.2testing版生成的新的androidTest示例如下所示:

 @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { @Test public void useAppContext() throws Exception { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("org.mypackage", appContext.getPackageName()); } } 

就像弃用警告所build议的那样,新的检测testing应该使用InstrumentationRegistry而不是从AndroidTestCase进行扩展。 用AndroidJUnit4运行它们。

build.gradle的相关dependencies部分如下所示:

 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })