ApplicationTestCase deprecated in API level 24

后端 未结 2 1639
一生所求
一生所求 2021-02-06 23:32

I created a default empty project on Android Studio 2.1.2 with API 24. In the sample project, Google offers a depreciated class Applica

2条回答
  •  情话喂你
    2021-02-06 23:50

    The new androidTest example that the beta version of Android Studio 2.2 generates, look like this:

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

    Just like the deprecation warning suggests, the new instrumentation tests should use InstrumentationRegistry instead of extending from AndroidTestCase. Run them with AndroidJUnit4.

    The relevant dependencies section in build.gradle looks like this:

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

提交回复
热议问题