migrating-from-android-test-classes
Installation
SKILL.md
Migrating Off android.test.* — From the Platform's Legacy Test Classes to AndroidX Test
The android.test.* package shipped in the Android SDK (InstrumentationTestCase, ActivityInstrumentationTestCase2, AndroidTestCase, ServiceTestCase, MoreAsserts, TouchUtils, android.test.mock.*, …) is @Deprecated — the platform's own Javadoc routes every class to AndroidX Test. Old codebases still carry these, and they actively hold back JUnit4, Espresso, ActivityScenario, and the AndroidX runner. This skill is the class-by-class replacement map and the mechanical migration patterns.
When to use this skill
- The IDE flags
android.test.InstrumentationTestCase/AndroidTestCase/ActivityInstrumentationTestCase2/ServiceTestCase/MoreAsserts/ViewAsserts/TouchUtils/android.test.mock.*as@Deprecated. - A module's
testInstrumentationRunnerisandroid.test.InstrumentationTestRunnerand tests behave oddly underAndroidJUnitRunner(or won't be discovered). - The user asks how to replace a specific legacy base class: "
getInstrumentation()outside aTestCase", "getActivity()/setActivityIntent()withoutActivityInstrumentationTestCase2", "getContext()withoutAndroidTestCase". - A test extends
junit.framework.TestCase(JUnit3 style:setUp()override,testFoo()naming,assertEqualsfromjunit.framework.Assert) and the team wants JUnit4. - The user mentions
MoreAsserts.assertContentsInAnyOrder/assertEquals(int[], int[])/assertContainsRegex,ViewAsserts.assertOnScreen,TouchUtils.tapView/dragQuarterScreenDown, orandroid.test.suitebuilder.annotation.SmallTest.
When NOT to use this skill
- The codebase is already on AndroidX Test and the question is about using it (the runner,
ActivityScenario, Espresso,ServiceTestRule) — go straight to../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md,../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md,../../../instrumentation/espresso/writing-espresso-tests/SKILL.md. - The question is about
kotlin.testassertions (assertEquals,assertFailsWith) — use../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.md. (kotlin.testis one valid target forMoreAssertscalls.) - The legacy class in question is a Robolectric shadow or a JUnit3 non-Android
TestCasein pure-JVM code — for the JVM/Robolectric side use../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.mdand../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md. - The user wants to write a new test — do not start from
android.test.*at all; use the AndroidX skills directly.