Loading...
Loading...
Use this skill to migrate a codebase off the deprecated `android.test.*` testing classes that ship in the Android platform SDK onto AndroidX Test. Covers `InstrumentationTestRunner` → `AndroidJUnitRunner`; `InstrumentationTestCase`/`AndroidTestCase` → `@RunWith(AndroidJUnit4)` + `InstrumentationRegistry`/`ApplicationProvider`; `ActivityInstrumentationTestCase2` → `ActivityScenario`/`ActivityScenarioRule` + Espresso; `ServiceTestCase`/`ProviderTestCase2` → `ServiceTestRule`/`ProviderTestRule`; `MoreAsserts` → Truth/Hamcrest; `ViewAsserts`/`TouchUtils` → Espresso matchers/actions; `android.test.UiThreadTest`/`FlakyTest`/`suitebuilder.annotation.*` → `androidx.test.*`; `android.test.mock.*` → real test contexts or Mockito; and JUnit3 `extends TestCase` → JUnit4 `@Test`. Use when the user sees `@Deprecated` on `android.test.*`, asks how to replace `ActivityInstrumentationTestCase2` / `InstrumentationTestCase` / `MoreAsserts` / `TouchUtils`, or "old Android tests won't run on AndroidJUnitRunner".
npx skill4agent add skydoves/android-testing-skills migrating-from-android-test-classesandroid.test.*InstrumentationTestCaseActivityInstrumentationTestCase2AndroidTestCaseServiceTestCaseMoreAssertsTouchUtilsandroid.test.mock.*@DeprecatedActivityScenarioandroid.test.InstrumentationTestCaseAndroidTestCaseActivityInstrumentationTestCase2ServiceTestCaseMoreAssertsViewAssertsTouchUtilsandroid.test.mock.*@DeprecatedtestInstrumentationRunnerandroid.test.InstrumentationTestRunnerAndroidJUnitRunnergetInstrumentation()TestCasegetActivity()setActivityIntent()ActivityInstrumentationTestCase2getContext()AndroidTestCasejunit.framework.TestCasesetUp()testFoo()assertEqualsjunit.framework.AssertMoreAsserts.assertContentsInAnyOrderassertEquals(int[], int[])assertContainsRegexViewAsserts.assertOnScreenTouchUtils.tapViewdragQuarterScreenDownandroid.test.suitebuilder.annotation.SmallTestActivityScenarioServiceTestRule../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md../../../instrumentation/espresso/writing-espresso-tests/SKILL.mdkotlin.testassertEqualsassertFailsWith../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.mdkotlin.testMoreAssertsTestCase../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.mdandroid.test.*../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdandroidx.test:core:runner:rulesandroidx.test.ext:junit../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.mdjunit:junit:4.13.2kotlin("test")extends TestCase@Test@RunWithandroidx.test.espresso:espresso-coreTouchUtilsViewAssertsActivityInstrumentationTestCase2Legacy | Use instead | Notes |
|---|---|---|
| | Change |
| | |
| | |
| | No 1:1 base class — the |
| | |
| (Fragment cases, if any home-grown) | | |
| | |
| | |
| (Loaders are themselves deprecated) | Migrate the Loader to a |
| Google Truth ( | The platform Javadoc says "use Hamcrest"; Truth reads better. |
| Espresso | See |
| Espresso | The legacy methods need a live |
| | Just change the import; or replace with |
| | Import change. |
| | Import change; the runner reads |
| JUnit4 | Manual suite building is rarely needed once the AndroidX runner is in place. |
| A real context ( | |
| | If you genuinely need a stub resolver, Mockito a |
| Mockito/MockK | The platform Javadoc explicitly says "use a mocking framework like Mockito". |
| Mockito/MockK mocks, or real instances from a test context | Same story — these were stub bases, mocking frameworks supersede them. |
| | |
| | Method names no longer need a |
The platform's ownJavadoc often points at the old "Android Testing Support Library" /@deprecatednames (e.g.android.support.test.*). Those becameActivityTestRule, and several have since been re-deprecated again —androidx.test.*→ActivityTestRule. Migrate to the current target in the table above, not to whatever the platform Javadoc literally says.ActivityScenario
android.defaultConfig.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"ActivityScenario../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdextends …TestCase@RunWith(AndroidJUnit4::class)@TestsetUp()tearDown()@Before@AftersupertestFoofoogetContext()ApplicationProvider.getApplicationContext()getInstrumentation()InstrumentationRegistry.getInstrumentation()getActivity()ActivityScenarioonActivity { }getService()ServiceTestRule.bindService(...)MoreAsserts.*kotlin.testViewAsserts.*ViewAssertionsTouchUtils.*ViewActionsandroid.test.mock.MockContextMockResourcesMockPackageManager../../../jvm-tests/mocking/mocking-with-mockito/SKILL.mdandroid.test.*android.test.mock.*import android.test.ActivityInstrumentationTestCase2ActivityScenario// WRONG — deprecated base class
class LoginActivityTest : ActivityInstrumentationTestCase2<LoginActivity>(LoginActivity::class.java) {
fun testTitleShown() {
val activity = activity // getActivity()
assertEquals("Sign in", activity.title)
}
}
// WRONG because: android.test.ActivityInstrumentationTestCase2 is @Deprecated; it ties the test to
// the legacy InstrumentationTestRunner, blocks JUnit4/Espresso, and getActivity() leaks the Activity
// off the main thread.// RIGHT — ActivityScenario + JUnit4
@RunWith(AndroidJUnit4::class)
class LoginActivityTest {
@get:Rule val scenario = ActivityScenarioRule(LoginActivity::class.java)
@Test fun titleShown() {
scenario.scenario.onActivity { activity ->
assertEquals("Sign in", activity.title)
}
onView(withId(R.id.title)).check(matches(withText("Sign in"))) // or assert via Espresso
}
}AndroidTestCase@RunWith(AndroidJUnit4)ApplicationProvider// WRONG
class FormatterTest : AndroidTestCase() {
fun testCurrency() {
val s = CurrencyFormatter(context).format(1099) // getContext()
assertEquals("$10.99", s)
}
}
// WRONG because: android.test.AndroidTestCase is @Deprecated; getContext() and the TestCase shape
// are the legacy world.// RIGHT
@RunWith(AndroidJUnit4::class)
class FormatterTest {
private val context: Context = ApplicationProvider.getApplicationContext()
@Test fun currency() {
assertEquals("$10.99", CurrencyFormatter(context).format(1099))
}
}MoreAssertsTouchUtils// WRONG
MoreAsserts.assertContentsInAnyOrder(ids, 3L, 1L, 2L) // android.test.MoreAsserts (deprecated)
TouchUtils.tapView(this, listView) // android.test.TouchUtils (deprecated)
ViewAsserts.assertOnScreen(rootView, listView) // android.test.ViewAsserts (deprecated)// RIGHT
assertThat(service.ids()).containsExactly(1L, 2L, 3L) // Google Truth (order-insensitive)
onView(withId(R.id.list)).perform(click()) // Espresso ViewActions
onView(withId(R.id.list)).check(matches(isCompletelyDisplayed())) // Espresso ViewAssertionsandroid.test.mock.MockContext// WRONG
class FakeContext : MockContext() { // android.test.mock.MockContext (deprecated)
override fun getPackageName() = "com.example"
// every other method throws UnsupportedOperationException
}// RIGHT — a real test context if the SUT just reads from it
val context: Context = ApplicationProvider.getApplicationContext()
// RIGHT — a mock if you must control specific calls in isolation
val context = mock<Context> { on { packageName } doReturn "com.example" }android.test.*android.test.mock.*@DeprecatedAndroidJUnit4ActivityScenarioServiceTestRuleProviderTestRuleorg.junitkotlin.testtestInstrumentationRunnerandroidx.test.runner.AndroidJUnitRunnerInstrumentationTestRunnergetActivity()setActivityIntent()ActivityScenariolaunchonActivity { }ActivityTestRuleActivityTestRuleandroid.test.mock.MockContextMockResourcesMockPackageManagerMockContentResolverMockContextextends TestCasetestFoo()setUp()tearDown()junit.framework.Assert@RunWith(AndroidJUnit4)@Test@Before@Afterorg.junit.Assertkotlin.test@deprecatedandroid.support.test.*android.test.*androidx.test.ext:truthMoreAssertsgit grep -n 'import android.test\.' -- '*.java' '*.kt'testInstrumentationRunnerandroid.test.InstrumentationTestRunnerandroidx.test.runner.AndroidJUnitRunnerextends ActivityInstrumentationTestCase2AndroidTestCaseInstrumentationTestCaseServiceTestCaseProviderTestCase2junit.framework.TestCaseMoreAsserts.ViewAsserts.TouchUtils.android.test.mock.MockContextMockResourcesMockPackageManagerMockContentResolver./gradlew :module:connectedDebugAndroidTest./gradlew :module:testDebugUnitTestandroid.test.*android.testframeworks/base/test-base/src/android/test/AndroidTestCase.javaInstrumentationTestCase.javaUiThreadTest.javaFlakyTest.java@DeprecatedInstrumentationRegistryandroidx.test.annotation.UiThreadTestandroidx.test.filters.FlakyTestframeworks/base/test-runner/src/android/test/ActivityInstrumentationTestCase2.javaServiceTestCase.javaProviderTestCase2.javaInstrumentationTestRunner.javaMoreAsserts.javaViewAsserts.javaTouchUtils.javaActivityTestRuleServiceTestRuleAndroidJUnitRunnerframeworks/base/test-mock/src/android/test/mock/MockContext.javaMockPackageManager.javaMockContentResolver.javaMockResources.javaMockCursor.javaMockPackageManager../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdAndroidJUnitRunner../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.mdActivityScenarioActivityScenarioRuleActivityInstrumentationTestCase2../../../instrumentation/scenarios/launching-fragments-with-fragmentscenario/SKILL.mdFragmentScenario../../../instrumentation/espresso/writing-espresso-tests/SKILL.mdViewMatchersViewActionsViewAssertionsViewAssertsTouchUtils../../../jvm-tests/mocking/mocking-with-mockito/SKILL.mdContextPackageManagerResourcesandroid.test.mock.*../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.mdkotlin.testassertEqualsassertContentEqualsassertFailsWithMoreAssertsjunit.framework.Assert../../../fundamentals/strategies/organizing-test-source-sets/SKILL.mdsrc/test/src/androidTest/