Loading...
Loading...
Compare original and translation side by side
AndroidJUnit4InstrumentationRegistryandroidx.test:coreMainDispatcherRuletestutils-ktxAndroidJUnit4InstrumentationRegistryandroidx.test:coretestutils-ktxMainDispatcherRuleCannot resolve symbol AndroidJUnit4AndroidJUnit4 is deprecatedAndroidJUnit4IllegalStateException: No instrumentation registeredInstrumentationRegistry.getInstrumentation()InstrumentationRegistryIntentSubjectBundleSubjectLocationSubjectParcelableSubjectam instrument -e size small@SmallTest@MediumTest@LargeTestViewModelMainDispatcherRuleCannot resolve symbol AndroidJUnit4AndroidJUnit4 is deprecatedAndroidJUnit4InstrumentationRegistry.getInstrumentation()IllegalStateException: No instrumentation registeredInstrumentationRegistryIntentSubjectBundleSubjectLocationSubjectParcelableSubjectam instrument -e size small@SmallTest@MediumTest@LargeTestViewModelMainDispatcherRule../../mocking/mocking-with-mockito/SKILL.md../../mocking/mocking-with-mockk/SKILL.mdrunTest { … }Turbine../../coroutines/testing-coroutines-with-runtest/SKILL.md../../coroutines/testing-flows-with-turbine/SKILL.md@Config../../robolectric/using-robolectric-correctly/SKILL.mdam instrument../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../fundamentals/doubles/picking-test-doubles/SKILL.md../../mocking/mocking-with-mockito/SKILL.md../../mocking/mocking-with-mockk/SKILL.mdrunTest { … }Turbine../../coroutines/testing-coroutines-with-runtest/SKILL.md../../coroutines/testing-flows-with-turbine/SKILL.md@Config../../robolectric/using-robolectric-correctly/SKILL.mdam instrument../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../fundamentals/doubles/picking-test-doubles/SKILL.mdcom.android.applicationcom.android.librarysrc/test/mkdir -p src/test/kotlinorg.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.xcom.android.applicationcom.android.librarysrc/test/mkdir -p src/test/kotlinorg.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.x~/.gradle/caches/modules-2/files-2.1/androidx.test/...latest.releasedependencies {
// Core JUnit4
testImplementation("junit:junit:4.13.2")
// androidx.test foundations (work in src/test/ via Robolectric and in src/androidTest/)
testImplementation("androidx.test:core:1.7.0")
testImplementation("androidx.test:core-ktx:1.6.1")
testImplementation("androidx.test:runner:1.7.0")
testImplementation("androidx.test:rules:1.7.0")
testImplementation("androidx.test.ext:junit:1.3.0")
testImplementation("androidx.test.ext:junit-ktx:1.3.0")
testImplementation("androidx.test.ext:truth:1.7.0")
testImplementation("com.google.truth:truth:1.4.4")
// Coroutines test (for MainDispatcherRule / runTest)
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.1")
}src/androidTest/androidTestImplementation@RunWithAndroidJUnit4androidx.test.runner.AndroidJUnit4runner-1.7.0/androidx/test/runner/AndroidJUnit4.java@Deprecatedandroidx.test.ext.junit.runners.AndroidJUnit4androidx.test.ext:junit:1.3.0junit-1.3.0/androidx/test/ext/junit/runners/AndroidJUnit4.javaorg.robolectric.RobolectricTestRunnerandroidx.test.internal.runner.junit4.AndroidJUnit4ClassRunnerimport androidx.test.ext.junit.runners.AndroidJUnit4 // canonical
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MyTest { /* ... */ }InstrumentationRegistryandroidx.test.InstrumentationRegistry.platform.app.@Deprecated:monitor@InlineMeandroidx.test.platform.app.InstrumentationRegistrymonitor-1.8.0/androidx/test/platform/app/InstrumentationRegistry.javaimport androidx.test.platform.app.InstrumentationRegistry
val instrumentation = InstrumentationRegistry.getInstrumentation()
val args: Bundle = InstrumentationRegistry.getArguments() // copy of -e key/value pairsgetInstrumentation()IllegalStateException("No instrumentation registered! Must run under a registering instrumentation.")Instrumentationsrc/test/@RunWith(AndroidJUnit4::class)ApplicationProviderApplicationProvider.getApplicationContext()core-1.7.0/androidx/test/core/app/ApplicationProvider.javagetInstrumentation().getTargetContext().getApplicationContext()Applicationimport androidx.test.core.app.ApplicationProvider
val ctx: Context = ApplicationProvider.getApplicationContext()
val app: MyApp = ApplicationProvider.getApplicationContext() // unchecked cast@SmallTest<200ms@MediumTest<1000ms@LargeTest>1000msandroidx.test.filters.*runner-1.7.0/androidx/test/filters/SmallTest.javaRunnerArgs.SIZErunner-1.7.0/androidx/test/internal/runner/RunnerArgs.java-e size <value>import androidx.test.filters.SmallTest
import androidx.test.filters.MediumTest
@SmallTest
class FastValidatorTest { /* ... */ }
@MediumTest
class RoomDaoTest { /* ... */ }am instrumentadb shell am instrument -w -r -e size small <pkg>/androidx.test.runner.AndroidJUnitRunner../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdandroidx.test.ext:truthandroidx.test.ext:truth:1.7.0Subjectstatic assertThat(T actual)static Subject.Factory<T, S> name()| Subject | FQCN package | Asserts on |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
import androidx.test.ext.truth.content.IntentSubject.assertThat
import androidx.test.ext.truth.os.BundleSubject
@Test fun intent_routes_to_settings() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("settings://wifi"))
assertThat(intent).hasAction(Intent.ACTION_VIEW)
assertThat(intent).hasData(Uri.parse("settings://wifi"))
}MainDispatcherRuleDispatchers.MainviewModelScopeLiveDataDispatchers.Maintestutils/testutils-ktx/src/jvmMain/kotlin/androidx/testutils/MainDispatcherRule.jvm.ktimport kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
val testDispatcher: TestDispatcher = StandardTestDispatcher(),
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description) {
Dispatchers.resetMain()
}
}@RunWith(AndroidJUnit4::class)
class MyViewModelTest {
@get:Rule val mainDispatcherRule = MainDispatcherRule()
@Test fun loadsUsers() = runTest {
val vm = MyViewModel(repo = FakeUserRepository())
vm.load()
advanceUntilIdle()
assertThat(vm.users.value).isNotEmpty()
}
}StandardTestDispatcherrunCurrent()advanceUntilIdle()kotlinx.coroutines.test.runTestUnconfinedTestDispatcher./gradlew :<module>:testDebugUnitTest --tests com.example.MyTestorg.junit.runner.RunWith: AndroidJUnit4Test process exited with status code~/.gradle/caches/modules-2/files-2.1/androidx.test/...latest.releasedependencies {
// Core JUnit4
testImplementation("junit:junit:4.13.2")
// androidx.test foundations (work in src/test/ via Robolectric and in src/androidTest/)
testImplementation("androidx.test:core:1.7.0")
testImplementation("androidx.test:core-ktx:1.6.1")
testImplementation("androidx.test:runner:1.7.0")
testImplementation("androidx.test:rules:1.7.0")
testImplementation("androidx.test.ext:junit:1.3.0")
testImplementation("androidx.test.ext:junit-ktx:1.3.0")
testImplementation("androidx.test.ext:truth:1.7.0")
testImplementation("com.google.truth:truth:1.4.4")
// Coroutines test (for MainDispatcherRule / runTest)
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.1")
}src/androidTest/androidTestImplementation@RunWithAndroidJUnit4androidx.test.runner.AndroidJUnit4runner-1.7.0/androidx/test/runner/AndroidJUnit4.java@Deprecatedandroidx.test.ext:junit:1.3.0androidx.test.ext.junit.runners.AndroidJUnit4junit-1.3.0/androidx/test/ext/junit/runners/AndroidJUnit4.javaorg.robolectric.RobolectricTestRunnerandroidx.test.internal.runner.junit4.AndroidJUnit4ClassRunnerimport androidx.test.ext.junit.runners.AndroidJUnit4 // canonical
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MyTest { /* ... */ }InstrumentationRegistryandroidx.test.InstrumentationRegistry.platform.app.:monitor@Deprecated@InlineMeandroidx.test.platform.app.InstrumentationRegistrymonitor-1.8.0/androidx/test/platform/app/InstrumentationRegistry.javaimport androidx.test.platform.app.InstrumentationRegistry
val instrumentation = InstrumentationRegistry.getInstrumentation()
val args: Bundle = InstrumentationRegistry.getArguments() // copy of -e key/value pairsgetInstrumentation()IllegalStateException("No instrumentation registered! Must run under a registering instrumentation.")Instrumentation@RunWith(AndroidJUnit4::class)src/test/ApplicationProviderApplicationProvider.getApplicationContext()core-1.7.0/androidx/test/core/app/ApplicationProvider.javagetInstrumentation().getTargetContext().getApplicationContext()Applicationimport androidx.test.core.app.ApplicationProvider
val ctx: Context = ApplicationProvider.getApplicationContext()
val app: MyApp = ApplicationProvider.getApplicationContext() // unchecked cast@SmallTest<200ms@MediumTest<1000ms@LargeTest>1000msandroidx.test.filters.*runner-1.7.0/androidx/test/filters/SmallTest.javaRunnerArgs.SIZErunner-1.7.0/androidx/test/internal/runner/RunnerArgs.java-e size <value>import androidx.test.filters.SmallTest
import androidx.test.filters.MediumTest
@SmallTest
class FastValidatorTest { /* ... */ }
@MediumTest
class RoomDaoTest { /* ... */ }am instrumentadb shell am instrument -w -r -e size small <pkg>/androidx.test.runner.AndroidJUnitRunner../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdandroidx.test.ext:truthandroidx.test.ext:truth:1.7.0Subjectstatic assertThat(T actual)static Subject.Factory<T, S> name()| Subject | FQCN package | 断言对象 |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
import androidx.test.ext.truth.content.IntentSubject.assertThat
import androidx.test.ext.truth.os.BundleSubject
@Test fun intent_routes_to_settings() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("settings://wifi"))
assertThat(intent).hasAction(Intent.ACTION_VIEW)
assertThat(intent).hasData(Uri.parse("settings://wifi"))
}Dispatchers.MainMainDispatcherRuleviewModelScopeLiveDataDispatchers.Maintestutils/testutils-ktx/src/jvmMain/kotlin/androidx/testutils/MainDispatcherRule.jvm.ktimport kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
val testDispatcher: TestDispatcher = StandardTestDispatcher(),
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description) {
Dispatchers.resetMain()
}
}@RunWith(AndroidJUnit4::class)
class MyViewModelTest {
@get:Rule val mainDispatcherRule = MainDispatcherRule()
@Test fun loadsUsers() = runTest {
val vm = MyViewModel(repo = FakeUserRepository())
vm.load()
advanceUntilIdle()
assertThat(vm.users.value).isNotEmpty()
}
}StandardTestDispatcherrunCurrent()advanceUntilIdle()kotlinx.coroutines.test.runTestUnconfinedTestDispatcher./gradlew :<module>:testDebugUnitTest --tests com.example.MyTestorg.junit.runner.RunWith: AndroidJUnit4Test process exited with status codeAndroidJUnit4AndroidJUnit4// WRONG
import org.junit.runners.JUnit4
import org.junit.runner.RunWith
@RunWith(JUnit4::class)
class MyTest { /* uses InstrumentationRegistry, ApplicationProvider, ... */ }
// WRONG because: stock JUnit4 does not register the Android `Instrumentation` instance
// into InstrumentationRegistry. ApplicationProvider.getApplicationContext() throws
// IllegalStateException("No instrumentation registered!"). Robolectric is also never
// activated even when on the classpath, so all framework calls hit unmocked stubs.// RIGHT
import androidx.test.ext.junit.runners.AndroidJUnit4 // ext.junit.runners, NOT runner.AndroidJUnit4
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MyTest { /* ... */ }androidx.test.runner.AndroidJUnit4androidx.test.ext.junit.runners// WRONG
import org.junit.runners.JUnit4
import org.junit.runner.RunWith
@RunWith(JUnit4::class)
class MyTest { /* uses InstrumentationRegistry, ApplicationProvider, ... */ }
// WRONG because: stock JUnit4 does not register the Android `Instrumentation` instance
// into InstrumentationRegistry. ApplicationProvider.getApplicationContext() throws
// IllegalStateException("No instrumentation registered!"). Robolectric is also never
// activated even when on the classpath, so all framework calls hit unmocked stubs.// RIGHT
import androidx.test.ext.junit.runners.AndroidJUnit4 // ext.junit.runners, NOT runner.AndroidJUnit4
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MyTest { /* ... */ }androidx.test.runner.AndroidJUnit4androidx.test.ext.junit.runnersInstrumentationRegistryInstrumentationRegistry// WRONG
import androidx.test.InstrumentationRegistry // @Deprecated since :monitor
val ctx = InstrumentationRegistry.getContext() // also deprecated — not the Application context
// WRONG because: every member of androidx.test.InstrumentationRegistry is @Deprecated
// and @InlineMe'd to its replacement. getContext() returns the instrumentation package
// context, NOT the Application context — common source of "resource not found" failures.// RIGHT
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.core.app.ApplicationProvider
val ctx: Context = ApplicationProvider.getApplicationContext() // application context
val args: Bundle = InstrumentationRegistry.getArguments() // -e key/value pairs
val instr = InstrumentationRegistry.getInstrumentation()// WRONG
import androidx.test.InstrumentationRegistry // @Deprecated since :monitor
val ctx = InstrumentationRegistry.getContext() // also deprecated — not the Application context
// WRONG because: every member of androidx.test.InstrumentationRegistry is @Deprecated
// and @InlineMe'd to its replacement. getContext() returns the instrumentation package
// context, NOT the Application context — common source of "resource not found" failures.// RIGHT
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.core.app.ApplicationProvider
val ctx: Context = ApplicationProvider.getApplicationContext() // application context
val args: Bundle = InstrumentationRegistry.getArguments() // -e key/value pairs
val instr = InstrumentationRegistry.getInstrumentation()Dispatchers.MainDispatchers.Main// WRONG
class MyViewModelTest {
@Test fun loadsUsers() = runBlocking {
val vm = MyViewModel(repo = FakeUserRepository())
vm.load()
// Throws IllegalStateException: "Module with the Main dispatcher is missing"
}
}
// WRONG because: kotlinx-coroutines-android (the Main dispatcher impl for Android) is
// NOT on the unit-test classpath. Without Dispatchers.setMain(...), any viewModelScope or
// LaunchedEffect that touches the Main dispatcher fails immediately.// RIGHT
class MyViewModelTest {
@get:Rule val mainDispatcherRule = MainDispatcherRule()
@Test fun loadsUsers() = runTest {
val vm = MyViewModel(repo = FakeUserRepository())
vm.load()
advanceUntilIdle()
assertThat(vm.users.value).hasSize(2)
}
}// WRONG
class MyViewModelTest {
@Test fun loadsUsers() = runBlocking {
val vm = MyViewModel(repo = FakeUserRepository())
vm.load()
// Throws IllegalStateException: "Module with the Main dispatcher is missing"
}
}
// WRONG because: kotlinx-coroutines-android (the Main dispatcher impl for Android) is
// NOT on the unit-test classpath. Without Dispatchers.setMain(...), any viewModelScope or
// LaunchedEffect that touches the Main dispatcher fails immediately.// RIGHT
class MyViewModelTest {
@get:Rule val mainDispatcherRule = MainDispatcherRule()
@Test fun loadsUsers() = runTest {
val vm = MyViewModel(repo = FakeUserRepository())
vm.load()
advanceUntilIdle()
assertThat(vm.users.value).hasSize(2)
}
}@SmallTest@MediumTest@LargeTestRunnerArgs.SIZErunner-1.7.0/androidx/test/internal/runner/RunnerArgs.javaadb shell am instrument -w -r -e size small \
com.example.test/androidx.test.runner.AndroidJUnitRunner./gradlew :app:testDebugUnitTest --tests "com.example.*" \
-PandroidTestSize=small-e annotation com.example.SmokeTestRunnerArgs.ANNOTATION@SmallTest@MediumTest@LargeTestRunnerArgs.SIZErunner-1.7.0/androidx/test/internal/runner/RunnerArgs.javaadb shell am instrument -w -r -e size small \
com.example.test/androidx.test.runner.AndroidJUnitRunner./gradlew :app:testDebugUnitTest --tests "com.example.*" \
-PandroidTestSize=small-e annotation com.example.SmokeTestRunnerArgs.ANNOTATIONAndroidJUnit4androidx.test.ext.junit.runnersandroidx.test.runner.AndroidJUnit4@DeprecatedInstrumentationRegistryandroidx.test.platform.appandroidx.test.InstrumentationRegistry@DeprecatedApplicationProvider.getApplicationContext()InstrumentationRegistry.getInstrumentation().getTargetContext()ApplicationProviderMainDispatcherRuleDispatchers.setMainresetMainviewModelScopeLaunchedEffectDispatchers.MainIllegalStateException: Module with the Main dispatcher is missingStandardTestDispatcherUnconfinedTestDispatcherMainDispatcherRuleStandardTestDispatcherrunTest@SmallTest@MediumTest@LargeTestorg.robolectric:robolectric:4.xcompileSdkrunBlockingTest { }@Deprecated(level = DeprecationLevel.ERROR)kotlinx-coroutines-testrunTest { }ActivityTestRule<A>@Deprecatedandroidx.test.ext.junit.rules.ActivityScenarioRule<A>junit-1.3.0/androidx/test/ext/junit/rules/ActivityScenarioRule.java../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdtestOptions.unitTests.isIncludeAndroidResources = trueandroid { … }R.string.*androidx.test.ext.junit.runnersAndroidJUnit4androidx.test.runner.AndroidJUnit4@Deprecatedandroidx.test.platform.appInstrumentationRegistryandroidx.test.InstrumentationRegistry@DeprecatedApplicationProvider.getApplicationContext()InstrumentationRegistry.getInstrumentation().getTargetContext()ApplicationProviderviewModelScopeLaunchedEffectDispatchers.MainMainDispatcherRuleDispatchers.setMainresetMainIllegalStateException: Module with the Main dispatcher is missingMainDispatcherRuleStandardTestDispatcherUnconfinedTestDispatcherStandardTestDispatcherrunTest@SmallTest@MediumTest@LargeTestcompileSdkorg.robolectric:robolectric:4.xrunBlockingTest { }kotlinx-coroutines-test@Deprecated(level = DeprecationLevel.ERROR)runTest { }ActivityTestRule<A>androidx.test.ext.junit.rules.ActivityScenarioRule<A>junit-1.3.0/androidx/test/ext/junit/rules/ActivityScenarioRule.java../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.mdandroid { … }testOptions.unitTests.isIncludeAndroidResources = trueR.string.*./gradlew :<module>:testDebugUnitTestTests run: N, Failures: 0grep -r "import androidx.test.runner.AndroidJUnit4" src/test src/androidTestandroidx.test.ext.junit.runners.AndroidJUnit4grep -r "import androidx.test.InstrumentationRegistry" src/test src/androidTestviewModelScopeDispatchers.Main@get:Rule val mainDispatcherRule = MainDispatcherRule()runTest { … }@SmallTest@MediumTest@LargeTest./gradlew :<module>:testDebugUnitTest --infoorg.robolectric.RobolectricTestRunnerAndroidJUnit4ClassRunnerAndroidJUnit4./gradlew :<module>:testDebugUnitTestTests run: N, Failures: 0grep -r "import androidx.test.runner.AndroidJUnit4" src/test src/androidTestandroidx.test.ext.junit.runners.AndroidJUnit4grep -r "import androidx.test.InstrumentationRegistry" src/test src/androidTestviewModelScopeDispatchers.Main@get:Rule val mainDispatcherRule = MainDispatcherRule()runTest { … }@SmallTest@MediumTest@LargeTest./gradlew :<module>:testDebugUnitTest --infoorg.robolectric.RobolectricTestRunnerAndroidJUnit4ClassRunnerAndroidJUnit4runner-1.7.0/androidx/test/runner/AndroidJUnit4.java@Deprecatedjunit-1.3.0/androidx/test/ext/junit/runners/AndroidJUnit4.javamonitor-1.8.0/androidx/test/platform/app/InstrumentationRegistry.javagetInstrumentationgetArgumentsregisterInstancemonitor-1.8.0/androidx/test/InstrumentationRegistry.java@Deprecatedcore-1.7.0/androidx/test/core/app/ApplicationProvider.javarunner-1.7.0/androidx/test/filters/SmallTest.javaMediumTest.javaLargeTest.javarunner-1.7.0/androidx/test/internal/runner/RunnerArgs.java-e <key> <value>sizeannotationclasspackagenumShardsshardIndextestutils/testutils-ktx/src/jvmMain/kotlin/androidx/testutils/MainDispatcherRule.jvm.ktMainDispatcherRule../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.mdkotlin.testassertEqualsassertFailsWith@BeforeTestcommonTestrunner-1.7.0/androidx/test/runner/AndroidJUnit4.java@Deprecatedjunit-1.3.0/androidx/test/ext/junit/runners/AndroidJUnit4.javamonitor-1.8.0/androidx/test/platform/app/InstrumentationRegistry.javagetInstrumentationgetArgumentsregisterInstancemonitor-1.8.0/androidx/test/InstrumentationRegistry.javacore-1.7.0/androidx/test/core/app/ApplicationProvider.javarunner-1.7.0/androidx/test/filters/SmallTest.javaMediumTest.javaLargeTest.javarunner-1.7.0/androidx/test/internal/runner/RunnerArgs.java-e <key> <value>sizeannotationclasspackagenumShardsshardIndextestutils/testutils-ktx/src/jvmMain/kotlin/androidx/testutils/MainDispatcherRule.jvm.ktMainDispatcherRule../../../kotlin/kotlin-test/writing-tests-with-kotlin-test/SKILL.mdkotlin.testassertEqualsassertFailsWith@BeforeTestcommonTest