spekframework / spek

A specification framework for Kotlin
Other
2.23k stars 179 forks source link

Using Spek with UI tests on Android #945

Open rgal75 opened 3 years ago

rgal75 commented 3 years ago

I am trying to use Spek 2 for UI testing on Android but I always get an ExceptionInInitializerError when trying to launch an Activity with ActivityScenario.launch(...):

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

My dependencies are:

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

My spec:

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

Is there a sample project that shows how to use Spek 2 with instrumented testing on Android?

raniejade commented 3 years ago

Sorry for the delay! I'm not sure if it is totally possible at the moment, do mind I'm no android dev. Have you tried looking at https://github.com/mannodermaus/android-junit5? The instrumentation test part of the doc looks interesting. Spek provides a JUnit Platform engine (spek-runner-junit5) which might work with it.

rgal75 commented 3 years ago

Thanks, I will look into https://github.com/mannodermaus/android-junit5.