Android Fragments launchFragmentInContainer无法解析Android中的活动

chy5wohz  于 2022-12-13  发布在  Android
关注(0)|答案(6)|浏览(158)

在编写一个使用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似乎是有效的,但是值是用红色写的,所以我假设targetPackagename有问题。
如何消除此错误并使用launchFragmentInContainer在OneFragment上运行一个简单的测试?

pn9klfpd

pn9klfpd1#

该错误与我在Gradle上导入依赖项的方式有关。
之前:

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")

之后:

debugImplementation("androidx.fragment:fragment-testing:1.1.0-beta01")
debugImplementation("androidx.fragment:fragment-ktx:1.1.0-beta01")
debugImplementation("androidx.test:core:1.2.0")
debugImplementation("androidx.test:rules:1.2.0")
debugImplementation("androidx.test:runner:1.2.0")

androidTestImplementation更改为debugImplementation,解决了问题。编译并运行,结果为绿色测试。

4ioopgfo

4ioopgfo2#

尝试以这种方式配置

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'
    }
hsvhsicv

hsvhsicv3#

对我很有效

def fragmentx_version = "1.1.0"
implementation "androidx.fragment:fragment:$fragmentx_version"
debugImplementation ("androidx.fragment:fragment-testing:$fragmentx_version"){
     exclude group: 'androidx.test', module: 'core'
}
debugImplementation 'androidx.test:core-ktx:1.2.0'

@Test
fun verifyMap() {
     FragmentScenario.launchInContainer(HomeFragment::class.java)

     onView(withId(R.id.map)).check(matches(isDisplayed()))
}
yiytaume

yiytaume4#

我参考了Espresso的测试示例,并在应用级build.gradle文件中使用了以下一组依赖项

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:' + rootProject.androidxCompatVersion
implementation 'androidx.core:core-ktx:' + rootProject.androidxCoreVersion
implementation 'androidx.fragment:fragment-ktx:' + rootProject.androidxFragmentVersion

// Ideally this would only be present in test scope (Adding this fixed the issue for me)
debugImplementation 'androidx.fragment:fragment-testing:' + rootProject.androidxFragmentVersion
debugImplementation 'androidx.test:core:' + rootProject.coreVersion

testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
testImplementation 'androidx.test:core:' + rootProject.coreVersion
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
testAnnotationProcessor 'com.google.auto.service:auto-service:1.0-rc4'

androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
androidTestImplementation 'androidx.fragment:fragment-testing:' + rootProject.androidxFragmentVersion
androidTestImplementation 'org.robolectric:annotations:' + rootProject.robolectricVersion     }

在您的项目级构建.gradle文件中

ext {
buildToolsVersion = "28.0.3"
androidxCoreVersion = "1.1.0-rc02"
androidxCompatVersion = "1.1.0-rc01"
androidxFragmentVersion = "1.1.0-rc01"
coreVersion = "1.3.0-alpha03"
extJUnitVersion = "1.1.2-alpha03"
runnerVersion = "1.3.0-alpha03"
rulesVersion = "1.3.0-alpha03"
espressoVersion = "3.3.0-alpha03"
robolectricVersion = "4.3.1"   }

内部隔离片段测试类

@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class ExampleFragmentTest{
@Test
fun launchFragmentAndVerifyTheUI(){
   /* This is used to launch the fragment in an isolated environment ( This is essentially an empty activity that
   houses that fragment ) */
   launchFragmentInContainer<ExampleFragment>()
    // Now using Espresso to verify the fragment's UI, i.e textview and verifying that its getting displayed
    onView(withId(R.id.textView)).check(matches(withText("I am a fragment")))
}  }
lndjwyie

lndjwyie5#

我在使用HiltTestActivity.kt时遇到了同样的错误,因为我将AndroidManifest.xml并行插入到了活动文件中。
我的修复方法:将AndroidManifest.xml移到java文件夹。

qybjjes1

qybjjes16#

公认的答案是(几乎)正确的--我将解释为什么。
因此,首先,您需要将androidTestImplementation更改为debugImplementation,但仅针对以下两个依赖项(不是全部):

androidx.fragment:fragment-testing
androidx.test:core

原因是什么?根本原因是FragmentScenarioreported here中的一个旧错误(自2020年以来)。
Google自己在自己的代码库中建议不要使用androidTestImplementation进行片段依赖性测试,更多详细信息请访问:
https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-test-doubles#7
引用重要部分:

// Testing code should not be included in the main code.
// Once https://issuetracker.google.com/128612536 is fixed this can be fixed.

implementation "androidx.fragment:fragment-testing:$fragmentVersion"
implementation "androidx.test:core:$androidXTestCoreVersion"

但理想情况下它应该是debugImplementation而不是implementation

相关问题