スポンサーリンク

Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.whatdo.test/androidx.activity.ComponentActivity }

スポンサーリンク

現象

AndroidのInstrumentedTestをComposeも利用して実行する場合に以下のエラーが発生しました.

java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.whatdo.test/androidx.activity.ComponentActivity }
	at androidx.test.core.app.InstrumentationActivityInvoker.startActivity(InstrumentationActivityInvoker.java:402)
	at androidx.test.core.app.InstrumentationActivityInvoker.startActivity(InstrumentationActivityInvoker.java:437)
	at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
	at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
	at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$0$ActivityScenarioRule(ActivityScenarioRule.java:70)
	at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda$0.get(ActivityScenarioRule.java:70)
	at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
	at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
	at androidx.compose.ui.test.junit4.AndroidComposeTestRule$AndroidComposeStatement.evaluateInner(AndroidComposeTestRule.android.kt:357)
	at androidx.compose.ui.test.junit4.AndroidComposeTestRule$AndroidComposeStatement.evaluate(AndroidComposeTestRule.android.kt:346)
	at androidx.compose.ui.test.junit4.android.EspressoLink$getStatementFor$1.evaluate(EspressoLink.android.kt:63)
	at androidx.compose.ui.test.junit4.IdlingResourceRegistry$getStatementFor$1.evaluate(IdlingResourceRegistry.jvm.kt:160)
	at androidx.compose.ui.test.junit4.android.ComposeRootRegistry$getStatementFor$1.evaluate(ComposeRootRegistry.android.kt:150)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runners.Suite.runChild(Suite.java:128)
	at org.junit.runners.Suite.runChild(Suite.java:27)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
	at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
	at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:444)
	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)

対策

分かりにくいログですが,必要なモジュールが足りていません.最終的に以下の依存性を追加しておけば動きました.テスト環境によっては必要ないものも含まれていると思いますので,適宜削ってください.

    //Local Unit Tests Dependency
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:4.3.1'

    //Instrumented Tests Dependency
    androidTestImplementation "androidx.test:core:1.4.0"
    androidTestImplementation "androidx.test:core-ktx:1.4.0"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
    androidTestImplementation "androidx.test.ext:truth:1.4.0"
    androidTestImplementation "androidx.test:runner:1.4.0"
    androidTestUtil "androidx.test:orchestrator:1.4.1"

    //Compose Test Dependency
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

以下を参考にしています.なんだかAndroidの公式サイトでも微妙に必要な依存性が違うのが気になります.

Test  |  Jetpack  |  Android Developers
Testing your Compose layout  |  Jetpack Compose  |  Android Developers
Build instrumented tests  |  Android Developers

ちなみに一番上のandroidx.testのdependencyをそのまま貼ると,espresso-coreの記載方法が間違っているのでSyncできず以下のエラーが発生します.

Failed to resolve: androidx.test:espresso:espresso-core

正しくはtest:espressoではなく,test.espressoですので,以下のように修正してください.(案外公式もミスがあるんだな・・・?)

Failed to resolve: androidx.test.espresso:espresso-core
タイトルとURLをコピーしました