ApplicationTestCase在API级别24中已弃用

时间:2016-07-11 13:18:49

标签: android testing android-testing

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

  

此类已在API级别24中弃用。使用ActivityTestRule   代替。应使用Android测试支持编写新测试   库。

示例:

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);
    }
}

我的问题:为什么Android测试用例现已弃用?如何用ActivityTestRule替换ApplicationTestCase?

修改

我尝试使用 Expresso ,但在 API 24 compileSdkVersion 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中添加此lib时:

// 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'
  

我的结论目前, Android测试用例 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'
}

3 个答案:

答案 0 :(得分:17)

Android Studio 2.2测试版生成的新 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());
    }
}

就像弃用警告所示,新的检测测试应该使用InstrumentationRegistry而不是从AndroidTestCase扩展。使用AndroidJUnit4运行它们。

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

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

答案 1 :(得分:0)

API文档中已指出,该API已被弃用,而使用InstrumentationRegistry.getTargetContext()会依次调用Application类的onCreate方法。

getTargetContext 将调用Android清单中定义的 ApplicationStartupService 类,如下所示。

    <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       <application
           android:name=".service.ApplicationStartupService"

 public class ApplicationStartupService extends Application
 {

       /**
         * Method initializes the application configuration
       */
       @Override
       public void onCreate(){

          super.onCreate();

          this.initResources()
       }

      private void initResource(){
          //do your application init work here.
      }
}

Test Class

  @RunWith(AndroidJUnit4.class)      
  public class ApplicationStartupServiceTest {

  @Test
  public void testResourcesAreInitializedd() throws Exception {
   //do your assertions here.
  }

https://developer.android.com/reference/android/test/ApplicationTestCase

答案 2 :(得分:0)

我可以用什么代替

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }
}