migrating-from-android-test-classes
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrating Off android.test.* — From the Platform's Legacy Test Classes to AndroidX Test
停止使用 android.test.*
——从平台旧版测试类迁移到 AndroidX Test
android.test.*The package shipped in the Android SDK (, , , , , , , …) is — the platform's own Javadoc routes every class to AndroidX Test. Old codebases still carry these, and they actively hold back JUnit4, Espresso, , and the AndroidX runner. This skill is the class-by-class replacement map and the mechanical migration patterns.
android.test.*InstrumentationTestCaseActivityInstrumentationTestCase2AndroidTestCaseServiceTestCaseMoreAssertsTouchUtilsandroid.test.mock.*@DeprecatedActivityScenarioAndroid SDK 中随附的 包(、、、、、、 等)已标记为 ——平台自身的 Javadoc 都会将每个类指向 AndroidX Test。旧代码库中仍保留着这些类,而它们实际上会阻碍采用 JUnit4、Espresso、 和 AndroidX runner。本 skill 提供逐类别的替换映射和机械化迁移模式。
android.test.*InstrumentationTestCaseActivityInstrumentationTestCase2AndroidTestCaseServiceTestCaseMoreAssertsTouchUtilsandroid.test.mock.*@DeprecatedActivityScenarioWhen to use this skill
何时使用此 skill
- The IDE flags /
android.test.InstrumentationTestCase/AndroidTestCase/ActivityInstrumentationTestCase2/ServiceTestCase/MoreAsserts/ViewAsserts/TouchUtilsasandroid.test.mock.*.@Deprecated - A module's is
testInstrumentationRunnerand tests behave oddly underandroid.test.InstrumentationTestRunner(or won't be discovered).AndroidJUnitRunner - The user asks how to replace a specific legacy base class: "outside a
getInstrumentation()", "TestCase/getActivity()withoutsetActivityIntent()", "ActivityInstrumentationTestCase2withoutgetContext()".AndroidTestCase - A test extends (JUnit3 style:
junit.framework.TestCaseoverride,setUp()naming,testFoo()fromassertEquals) and the team wants JUnit4.junit.framework.Assert - The user mentions /
MoreAsserts.assertContentsInAnyOrder/assertEquals(int[], int[]),assertContainsRegex,ViewAsserts.assertOnScreen/TouchUtils.tapView, ordragQuarterScreenDown.android.test.suitebuilder.annotation.SmallTest
- IDE 将 /
android.test.InstrumentationTestCase/AndroidTestCase/ActivityInstrumentationTestCase2/ServiceTestCase/MoreAsserts/ViewAsserts/TouchUtils标记为android.test.mock.*。@Deprecated - 某模块的 是
testInstrumentationRunner,且测试在android.test.InstrumentationTestRunner下表现异常(或无法被发现)。AndroidJUnitRunner - 用户询问如何替换特定的旧版基类:“在 之外使用
TestCase”、“不使用getInstrumentation()时如何调用ActivityInstrumentationTestCase2/getActivity()”、“不使用setActivityIntent()时如何调用AndroidTestCase”。getContext() - 某个测试继承 (JUnit3 风格:覆盖
junit.framework.TestCase、采用setUp()命名,并使用testFoo()的junit.framework.Assert),而团队希望迁移到 JUnit4。assertEquals - 用户提到 /
MoreAsserts.assertContentsInAnyOrder/assertEquals(int[], int[])、assertContainsRegex、ViewAsserts.assertOnScreen/TouchUtils.tapView或dragQuarterScreenDown。android.test.suitebuilder.annotation.SmallTest
When NOT to use this skill
何时不应使用此 skill
- The codebase is already on AndroidX Test and the question is about using it (the runner, , Espresso,
ActivityScenario) — go straight toServiceTestRule,../../../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 assertions (
kotlin.test,assertEquals) — useassertFailsWith. (../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.mdis one valid target forkotlin.testcalls.)MoreAsserts - The legacy class in question is a Robolectric shadow or a JUnit3 non-Android in pure-JVM code — for the JVM/Robolectric side use
TestCaseand../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md.../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md - The user wants to write a new test — do not start from at all; use the AndroidX skills directly.
android.test.*
- 代码库已经使用 AndroidX Test,且问题是如何使用它(runner、、Espresso、
ActivityScenario)——直接前往ServiceTestRule、../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md、../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md。../../../instrumentation/espresso/writing-espresso-tests/SKILL.md - 问题涉及 断言(
kotlin.test、assertEquals)——使用assertFailsWith。(../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.md是kotlin.test调用的一个有效替代方案。)MoreAsserts - 所涉及的旧版类是纯 JVM 代码中的 Robolectric shadow 或 JUnit3 非 Android ——对于 JVM/Robolectric 侧,使用
TestCase和../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md。../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md - 用户想编写新测试——不要从 开始;直接使用 AndroidX skills。
android.test.*
Prerequisites
前置条件
- AndroidX Test on the test classpath — see for the exact coordinate matrix (
../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md,androidx.test:core,:runner,:rules). For host/JVM tests,androidx.test.ext:junit.../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md - JUnit4 on the classpath (or via
junit:junit:4.13.2). The legacy classes are JUnit3-shaped (kotlin("test")); the migration target is JUnit4 (extends TestCase+@Test).@RunWith - Espresso () for anything that was using
androidx.test.espresso:espresso-core/TouchUtils/ViewAsserts's UI poking.ActivityInstrumentationTestCase2
- 测试 classpath 上需有 AndroidX Test——参见 获取确切的依赖坐标矩阵(
../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md、androidx.test:core、:runner、:rules)。对于主机/JVM 测试,请参阅androidx.test.ext:junit。../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md - 测试 classpath 上需有 JUnit4(,或通过
junit:junit:4.13.2引入)。旧版类是 JUnit3 形态(kotlin("test"));迁移目标是 JUnit4(extends TestCase+@Test)。@RunWith - 对于原先使用 /
TouchUtils/ViewAsserts操作 UI 的逻辑,需使用 Espresso(ActivityInstrumentationTestCase2)。androidx.test.espresso:espresso-core
The replacement map
替换映射
Legacy | 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
旧版 | 改用 | 说明 |
|---|---|---|
| | 将 |
| | |
| | |
| | 没有 1:1 的基类——现在 |
| | |
| Fragment 测试用例(如果有自行实现的部分) | | |
| | |
| | |
| (Loader 本身也已弃用) | 先将 Loader 迁移为 |
| Google Truth( | 平台 Javadoc 表示“使用 Hamcrest”;Truth 的可读性更好。 |
| Espresso | 参见 |
| Espresso | 旧版方法需要有效的 |
| | 只需更改 import;或替换为 |
| | 更改 import。 |
| | 更改 import;runner 会针对 AndroidX 注解读取 |
| JUnit4 | 有了 AndroidX runner 后,通常不再需要手动构建测试套件。 |
| 真实 context( | |
| | 如果确实需要 stub resolver,请使用 Mockito mock |
| Mockito/MockK | 平台 Javadoc 明确表示“使用 Mockito 等 mocking framework”。 |
| Mockito/MockK mock,或测试 context 中的真实实例 | 同理——这些都是 stub 基类,现已被 mocking framework 取代。 |
| | |
| | 方法名不再需要 |
平台自身的Javadoc 经常指向旧的“Android Testing Support Library” /@deprecated名称(例如android.support.test.*)。这些名称后来变成了ActivityTestRule,其中若干项之后又被重新弃用——androidx.test.*→ActivityTestRule。请迁移到上表中的当前目标,不要照搬平台 Javadoc 字面所写的内容。ActivityScenario
Workflow
工作流
-
1. Flip the runner first. Setand add the AndroidX Test deps. Until this is done, JUnit4/Espresso/
android.defaultConfig.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"cannot run. (ActivityScenario.)../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md -
2. Convert one test class at a time, JUnit3 → JUnit4. Drop, add
extends …TestCase, annotate test methods@RunWith(AndroidJUnit4::class), convert@Test/setUp()overrides totearDown()/@Before(no@Aftercall), and renamesuper→testFooif you like. Replace each legacy accessor with its AndroidX equivalent from the table.foo -
3. Replace base-class superpowers with explicit AndroidX entry points.→
getContext();ApplicationProvider.getApplicationContext()→getInstrumentation();InstrumentationRegistry.getInstrumentation()→getActivity()+ActivityScenario;onActivity { }→getService().ServiceTestRule.bindService(...) -
4. Swap the assertion helpers.→ Truth/
MoreAsserts.*;kotlin.test→ EspressoViewAsserts.*;ViewAssertions→ EspressoTouchUtils.*.ViewActions -
5. Swap the mock contexts. Anything extending/
android.test.mock.MockContext/MockResources→ a real test context where possible, otherwise a Mockito/MockK mock. (MockPackageManager.)../../../jvm-tests/mocking/mocking-with-mockito/SKILL.md -
6. Delete the legacy imports and confirm nothing referencesor
android.test.*any more. A clean module compiles with zeroandroid.test.mock.*lines.import android.test.
-
1. 首先切换 runner。 将并添加 AndroidX Test 依赖。在此完成之前,JUnit4/Espresso/
android.defaultConfig.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"无法运行。(ActivityScenario。)../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md -
2. 每次迁移一个测试类,从 JUnit3 → JUnit4。 移除,添加
extends …TestCase,为测试方法添加@RunWith(AndroidJUnit4::class),将@Test/setUp()覆盖方法转换为tearDown()/@Before(无需调用@After),如果愿意,可以将super重命名为testFoo。按照表格将每个旧版 accessor 替换为对应的 AndroidX 替代项。foo -
3. 用显式 AndroidX 入口替换基类自带的能力。→
getContext();ApplicationProvider.getApplicationContext()→getInstrumentation();InstrumentationRegistry.getInstrumentation()→getActivity()+ActivityScenario;onActivity { }→getService()。ServiceTestRule.bindService(...) -
4. 替换断言辅助方法。→ Truth/
MoreAsserts.*;kotlin.test→ EspressoViewAsserts.*;ViewAssertions→ EspressoTouchUtils.*。ViewActions -
5. 替换 mock 上下文。 任何继承/
android.test.mock.MockContext/MockResources的类 → 尽可能使用真实测试上下文,否则使用 Mockito/MockK mock。(MockPackageManager。)../../../jvm-tests/mocking/mocking-with-mockito/SKILL.md -
6. 删除旧版 import,并确认不再引用或
android.test.*。 完成清理的模块应在不存在任何android.test.mock.*行的情况下成功编译。import android.test.
Patterns
模式
Pattern: ActivityInstrumentationTestCase2
→ ActivityScenario
ActivityInstrumentationTestCase2ActivityScenario模式:ActivityInstrumentationTestCase2
→ ActivityScenario
ActivityInstrumentationTestCase2ActivityScenariokotlin
// 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.kotlin
// 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
}
}kotlin
// 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.kotlin
// 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
}
}Pattern: AndroidTestCase
→ @RunWith(AndroidJUnit4)
+ ApplicationProvider
AndroidTestCase@RunWith(AndroidJUnit4)ApplicationProvider模式:AndroidTestCase
→ @RunWith(AndroidJUnit4)
+ ApplicationProvider
AndroidTestCase@RunWith(AndroidJUnit4)ApplicationProviderkotlin
// 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.kotlin
// RIGHT
@RunWith(AndroidJUnit4::class)
class FormatterTest {
private val context: Context = ApplicationProvider.getApplicationContext()
@Test fun currency() {
assertEquals("$10.99", CurrencyFormatter(context).format(1099))
}
}kotlin
// 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.kotlin
// RIGHT
@RunWith(AndroidJUnit4::class)
class FormatterTest {
private val context: Context = ApplicationProvider.getApplicationContext()
@Test fun currency() {
assertEquals("$10.99", CurrencyFormatter(context).format(1099))
}
}Pattern: MoreAsserts
/ TouchUtils
calls
MoreAssertsTouchUtils模式:MoreAsserts
/ TouchUtils
调用
MoreAssertsTouchUtilskotlin
// 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)kotlin
// 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 ViewAssertionskotlin
// 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)kotlin
// 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 ViewAssertionsPattern: android.test.mock.MockContext
subclass → real context or Mockito
android.test.mock.MockContext模式:android.test.mock.MockContext
子类 → 真实 context 或 Mockito
android.test.mock.MockContextkotlin
// WRONG
class FakeContext : MockContext() { // android.test.mock.MockContext (deprecated)
override fun getPackageName() = "com.example"
// every other method throws UnsupportedOperationException
}kotlin
// 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" }kotlin
// WRONG
class FakeContext : MockContext() { // android.test.mock.MockContext (deprecated)
override fun getPackageName() = "com.example"
// every other method throws UnsupportedOperationException
}kotlin
// 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" }Mandatory rules
强制规则
- MUST NOT add new tests using any or
android.test.*class — all areandroid.test.mock.*. New tests use AndroidX Test (@Deprecated,AndroidJUnit4,ActivityScenario,ServiceTestRule, Espresso) andProviderTestRule/org.junitassertions.kotlin.test - MUST switch to
testInstrumentationRunnerbefore (or as the first step of) migrating any test class — the legacyandroidx.test.runner.AndroidJUnitRunnerdoes not run JUnit4/Espresso.InstrumentationTestRunner - MUST replace /
getActivity()withsetActivityIntent()(ActivityScenario,launch), not withonActivity { }—ActivityTestRuleis itself deprecated.ActivityTestRule - MUST replace /
android.test.mock.MockContext/MockResources/MockPackageManagersubclasses with a real test context where the SUT only reads from it, otherwise a Mockito/MockK mock — never re-derive a new hand-stub fromMockContentResolver.MockContext - MUST convert JUnit3 shape (,
extends TestCase,testFoo()/setUp()overrides,tearDown()) to JUnit4 (junit.framework.Assert,@RunWith(AndroidJUnit4),@Test/@Before,@After/org.junit.Assert) — do not leave a class half-migrated.kotlin.test - MUST NOT trust the platform Javadoc's literal target (it often names the obsolete
@deprecatedAPI); migrate to the current AndroidX target in this skill's table.android.support.test.* - PREFERRED: migrate one test class fully per change, with the runner flip landing first; a half-migrated module (some classes on , some on AndroidX) is hard to reason about.
android.test.* - PREFERRED: Google Truth (for the Android subjects) over Hamcrest as the
androidx.test.ext:truthreplacement — better failure messages.MoreAsserts
- 不得(MUST NOT):添加任何使用 或
android.test.*类的新测试——它们全部已标记为android.test.mock.*。新测试应使用 AndroidX Test(@Deprecated、AndroidJUnit4、ActivityScenario、ServiceTestRule、Espresso)以及ProviderTestRule/org.junit断言。kotlin.test - 必须(MUST):在迁移任何测试类之前(或作为迁移的第一步)将 切换到
testInstrumentationRunner——旧版androidx.test.runner.AndroidJUnitRunner无法运行 JUnit4/Espresso。InstrumentationTestRunner - 必须(MUST):将 /
getActivity()替换为setActivityIntent()(ActivityScenario、launch),而不是onActivity { }——ActivityTestRule本身也已弃用。ActivityTestRule - 必须(MUST):当 SUT 仅读取上下文时,将 /
android.test.mock.MockContext/MockResources/MockPackageManager子类替换为真实测试上下文;否则使用 Mockito/MockK mock——绝不能基于MockContentResolver重新实现新的手工 stub。MockContext - 必须(MUST):将 JUnit3 形态(、
extends TestCase、testFoo()/setUp()覆盖方法、tearDown())完整转换为 JUnit4(junit.framework.Assert、@RunWith(AndroidJUnit4)、@Test/@Before、@After/org.junit.Assert)——不能让测试类处于半迁移状态。kotlin.test - 不得(MUST NOT):直接信任平台 Javadoc 给出的字面目标(它通常指向已经过时的
@deprecatedAPI);应迁移到本 skill 表格中的当前 AndroidX 目标。android.support.test.* - 优先(PREFERRED):每次变更完整迁移一个测试类,并首先完成 runner 切换;半迁移模块(部分类仍使用 ,部分类已使用 AndroidX)很难理解和维护。
android.test.* - 优先(PREFERRED):使用 Google Truth(Android subject 使用 )替代 Hamcrest 作为
androidx.test.ext:truth的替代方案——它的失败消息更清晰。MoreAsserts
Verification
验证
- is empty (or limited to a tracked, justified holdout list).
git grep -n 'import android.test\.' -- '*.java' '*.kt' - No module's is
testInstrumentationRunner; all areandroid.test.InstrumentationTestRunner.androidx.test.runner.AndroidJUnitRunner - No test class /
extends ActivityInstrumentationTestCase2/AndroidTestCase/InstrumentationTestCase/ServiceTestCase/ProviderTestCase2.junit.framework.TestCase - No /
MoreAsserts./ViewAsserts.references remain.TouchUtils. - No subclass of /
android.test.mock.MockContext/MockResources/MockPackageManagerremains.MockContentResolver - Migrated instrumentation tests run green under ; migrated host tests under
./gradlew :module:connectedDebugAndroidTest../gradlew :module:testDebugUnitTest
- 的输出为空(或仅剩已登记且有合理理由的暂缓项列表)。
git grep -n 'import android.test\.' -- '*.java' '*.kt' - 没有模块的 是
testInstrumentationRunner;所有模块均使用android.test.InstrumentationTestRunner。androidx.test.runner.AndroidJUnitRunner - 没有测试类 /
extends ActivityInstrumentationTestCase2/AndroidTestCase/InstrumentationTestCase/ServiceTestCase/ProviderTestCase2。junit.framework.TestCase - 不再存在 /
MoreAsserts./ViewAsserts.引用。TouchUtils. - 不再存在 /
android.test.mock.MockContext/MockResources/MockPackageManager的子类。MockContentResolver - 迁移后的 instrumentation 测试可通过 成功运行;迁移后的主机测试可通过
./gradlew :module:connectedDebugAndroidTest成功运行。./gradlew :module:testDebugUnitTest
References
参考资料
- developer.android.com/training/testing — AndroidX Test overview; the migration destination for everything in .
android.test.* - developer.android.com/reference/android/test/package-summary — the package reference, every class marked deprecated with its replacement pointer.
android.test - ,
frameworks/base/test-base/src/android/test/AndroidTestCase.java,InstrumentationTestCase.java,UiThreadTest.java(AOSP) —FlakyTest.javaclasses whose Javadoc routes to@Deprecated/InstrumentationRegistry/androidx.test.annotation.UiThreadTest.androidx.test.filters.FlakyTest - ,
frameworks/base/test-runner/src/android/test/ActivityInstrumentationTestCase2.java,ServiceTestCase.java,ProviderTestCase2.java,InstrumentationTestRunner.java,MoreAsserts.java,ViewAsserts.java(AOSP) — the deprecated runner/base classes; Javadocs nameTouchUtils.java/ActivityTestRule/ServiceTestRule/Hamcrest/Espresso as replacements.AndroidJUnitRunner - ,
frameworks/base/test-mock/src/android/test/mock/MockContext.java,MockPackageManager.java,MockContentResolver.java,MockResources.java(AOSP) — the stub-context bases;MockCursor.java's Javadoc explicitly says "use a mocking framework like Mockito".MockPackageManager - Cross-set: — the
../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md/ AndroidX Test coordinate matrix to land first.AndroidJUnitRunner - Cross-set: —
../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md/ActivityScenario, the replacement forActivityScenarioRule.ActivityInstrumentationTestCase2 - Cross-set: —
../../../instrumentation/scenarios/launching-fragments-with-fragmentscenario/SKILL.mdfor any home-grown fragment-test bases.FragmentScenario - Cross-set: — Espresso
../../../instrumentation/espresso/writing-espresso-tests/SKILL.md/ViewMatchers/ViewActions, the replacement forViewAssertions/ViewAsserts.TouchUtils - Cross-set: — mocking
../../../jvm-tests/mocking/mocking-with-mockito/SKILL.md/Context/PackageManager, the replacement forResources.android.test.mock.* - Cross-set: — real Android stubs on the JVM, an alternative to a mock context.
../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md - Cross-set: —
../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.mdassertions (kotlin.test,assertEquals,assertContentEquals), one valid replacement forassertFailsWithandMoreAsserts.junit.framework.Assert - Cross-set: —
../../../fundamentals/strategies/organizing-test-source-sets/SKILL.mdvssrc/test/, which the migrated classes need to be sorted into.src/androidTest/
- developer.android.com/training/testing — AndroidX Test 概览;所有 内容的迁移目标。
android.test.* - developer.android.com/reference/android/test/package-summary — 包参考,其中每个类都标记为已弃用并指向替代项。
android.test - 、
frameworks/base/test-base/src/android/test/AndroidTestCase.java、InstrumentationTestCase.java、UiThreadTest.java(AOSP)— 标记为FlakyTest.java的类,其 Javadoc 指向@Deprecated/InstrumentationRegistry/androidx.test.annotation.UiThreadTest。androidx.test.filters.FlakyTest - 、
frameworks/base/test-runner/src/android/test/ActivityInstrumentationTestCase2.java、ServiceTestCase.java、ProviderTestCase2.java、InstrumentationTestRunner.java、MoreAsserts.java、ViewAsserts.java(AOSP)— 已弃用的 runner/基类;其 Javadoc 将TouchUtils.java/ActivityTestRule/ServiceTestRule/Hamcrest/Espresso 指定为替代项。AndroidJUnitRunner - 、
frameworks/base/test-mock/src/android/test/mock/MockContext.java、MockPackageManager.java、MockContentResolver.java、MockResources.java(AOSP)— stub context 基类;MockCursor.java的 Javadoc 明确表示“使用 Mockito 等 mocking framework”。MockPackageManager - 跨技能集:— 需要优先落地的
../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md/ AndroidX Test 依赖坐标矩阵。AndroidJUnitRunner - 跨技能集:—
../../../instrumentation/scenarios/launching-activities-with-activityscenario/SKILL.md/ActivityScenario,用于替换ActivityScenarioRule。ActivityInstrumentationTestCase2 - 跨技能集:—
../../../instrumentation/scenarios/launching-fragments-with-fragmentscenario/SKILL.md,用于替换自行实现的 fragment 测试基类。FragmentScenario - 跨技能集:— Espresso
../../../instrumentation/espresso/writing-espresso-tests/SKILL.md/ViewMatchers/ViewActions,用于替换ViewAssertions/ViewAsserts。TouchUtils - 跨技能集:— 使用 mocking framework 替换
../../../jvm-tests/mocking/mocking-with-mockito/SKILL.md。android.test.mock.* - 跨技能集:— JVM 上的真实 Android stub,是 mock context 的替代方案。
../../../jvm-tests/robolectric/using-robolectric-correctly/SKILL.md - 跨技能集:—
../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.md断言(kotlin.test、assertEquals、assertContentEquals),是assertFailsWith和MoreAsserts的有效替代方案。junit.framework.Assert - 跨技能集:—
../../../fundamentals/strategies/organizing-test-source-sets/SKILL.md与src/test/的区别,以及迁移后的测试类应如何分类。src/androidTest/