在 Android 上使用 Spek 进行 UI 测试

问题描述

我正在尝试在 Android 上使用 Spek 2 进行 UI 测试,但是在尝试使用 ActivityScenario.launch(...) 启动 Activity 时,我总是遇到 ExceptionInInitializerError:

java.lang.ExceptionInInitializerError
    at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:194)

我的依赖项是:

dependencies {
    def spek_version = "2.0.15"
    def fragment_version = "1.3.0-rc01"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.3.2'
    implementation 'androidx.navigation:navigation-ui:2.3.2'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.2'

    testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
    testImplementation "org.spekframework.spek2:spek-dsl-jvm:$spek_version"
    testImplementation "org.spekframework.spek2:spek-runner-junit5:$spek_version"
    // spek requires kotlin-reflect,omit when already in classpath
    testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testImplementation "com.google.truth:truth:1.1"

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
    androidTestImplementation "org.spekframework.spek2:spek-dsl-jvm:$spek_version"
    androidTestImplementation "org.spekframework.spek2:spek-runner-junit5:$spek_version"
    androidTestImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
}

我的规格:

object DiaryFragmentSpec: Spek({

    describe("DiaryFragment") {
        beforeEachTest {
            ActivityScenario.launch(MainActivity::class.java)
        }
        it("shows the text `This is Diary Fragment`") {
            onView(withId(R.id.text_diary)).check(matches(withText("This is Diary Fragment")))
        }
    }
})

有人能指出我如何将 Spek DSL 用于 Android 仪器测试的正确方向吗?

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)