launchFragmentInContainer 无法在 Android 中解析 Activity

launchFragmentInContainer unable to resolve Activity in Android

在编写使用 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是:

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.[=21= 有问题]

如何消除此错误并运行 使用 launchFragmentInContainer 对 OneFragment 进行简单测试?

该错误与我在 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 并解决了问题。编译运行,绿色测试结果

尝试这样配置

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

对我有用

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()))
}

我引用了 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     }

在您的项目级别 build.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"   }

在隔离片段测试中class

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

我遇到了与 HiltTestActivity.kt 相同的错误,因为我将 AndroidManifest.xml 与 activity 文件并行插入。

我的修复:将 AndroidManifest.xml 移动到 java 文件夹。