Bug*_*0id 9 android android-fragments android-testing androidx
在编写使用的简单测试时launchFragmentInContainer,我收到以下错误消息:
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.myapp.appname.debug/androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity (has extras) }
Run Code Online (Sandbox Code Playgroud)
基本测试类是:
class OneFragmentTest {
@Test
fun testOneFragmentState_checkTitleText() {
val args = Bundle().apply {
putString("dummyKey", "dummyValue")
}
launchFragmentInContainer<OneFragment>(args)
onView(withId(R.id.tv_title)).check(matches(withText("title here")))
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试更新AndroidManifest.xml以下内容:
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.myapp.appname.debug/androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity (has extras) }
Run Code Online (Sandbox Code Playgroud)
但标签似乎instrumentation有效,但值用红色书写,因此我认为targetPackageand 出了点问题name。
我如何摆脱这个错误,并在OneFragment上运行一个简单的测试launchFragmentInContainer?
尝试这样配置
debugImplementation('androidx.fragment:fragment-testing:1.1.0') {
// exclude androidx.test:core while fragment_testing depends on 1.1.0
exclude group: 'androidx.test', module: 'core'
}
Run Code Online (Sandbox Code Playgroud)
The error was related to the way I did import the dependencies on Gradle.
Before:
androidTestImplementation("androidx.fragment:fragment-testing:1.1.0-beta01")
implementation("androidx.fragment:fragment-ktx:1.1.0-beta01")
androidTestImplementation("androidx.test:core:1.2.0")
androidTestImplementation("androidx.test:rules:1.2.0")
androidTestImplementation("androidx.test:runner:1.2.0")
Run Code Online (Sandbox Code Playgroud)
After:
implementation("androidx.fragment:fragment-testing:1.1.0-beta01")
implementation("androidx.fragment:fragment-ktx:1.1.0-beta01")
implementation("androidx.test:core:1.2.0")
implementation("androidx.test:rules:1.2.0")
implementation("androidx.test:runner:1.2.0")
Run Code Online (Sandbox Code Playgroud)
Changed from androidTestImplementation to implementation and it solved the issue. Compiling and running, and green test as result.
| 归档时间: |
|
| 查看次数: |
931 次 |
| 最近记录: |