在编写一个使用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) }
基本测试类为:
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")))
}
}
我尝试使用以下内容更新AndroidManifest.xml
:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.myapp.appname" />
但是标签instrumentation
似乎是有效的,但是值是用红色写的,所以我假设targetPackage
和name
有问题。
如何消除此错误并使用launchFragmentInContainer
在OneFragment上运行一个简单的测试?
6条答案
按热度按时间pn9klfpd1#
该错误与我在Gradle上导入依赖项的方式有关。
之前:
之后:
将
androidTestImplementation
更改为debugImplementation
,解决了问题。编译并运行,结果为绿色测试。4ioopgfo2#
尝试以这种方式配置
hsvhsicv3#
对我很有效
yiytaume4#
我参考了Espresso的测试示例,并在应用级build.gradle文件中使用了以下一组依赖项
在您的项目级构建.gradle文件中
内部隔离片段测试类
lndjwyie5#
我在使用HiltTestActivity.kt时遇到了同样的错误,因为我将AndroidManifest.xml并行插入到了活动文件中。
我的修复方法:将AndroidManifest.xml移到java文件夹。
qybjjes16#
公认的答案是(几乎)正确的--我将解释为什么。
因此,首先,您需要将
androidTestImplementation
更改为debugImplementation
,但仅针对以下两个依赖项(不是全部):原因是什么?根本原因是
FragmentScenario
reported here中的一个旧错误(自2020年以来)。Google自己在自己的代码库中建议不要使用
androidTestImplementation
进行片段依赖性测试,更多详细信息请访问:https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-test-doubles#7
引用重要部分:
但理想情况下它应该是
debugImplementation
而不是implementation