微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Android 上使用 Spek 进行 UI 测试

如何解决在 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 举报,一经查实,本站将立刻删除。