Issue
I have an issue with Kotlin Espresso tests. I have applied Kotlin-Android
Gradle plugin in the module's build script, and the tests run without issues in Android Studio (the test class is annotated and run by AndroidJUnit4
runner), but running the Gradle task “assembleDebugAndroidTest" from terminal fails to find my main classes and packages (classes used in the tests). It throws an Unresolved Reference
error.
Here is the sourceSets section of the build script:
sourceSets {
androidTest {
java.srcDir 'src/sharedTestUtils/java'
java.srcDir 'src/acceptanceTests/java'
java.srcDir 'src/functionalTests/java'
java.srcDir 'src/sharedTestUtils/kotlin'
java.srcDir 'src/acceptanceTests/kotlin'
java.srcDir 'src/functionalTests/kotlin'
}
acceptanceTests {
java.srcDir 'src/sharedTestUtils/java'
java.srcDir 'src/sharedTestUtils/kotlin'
}
// source sets for Kotlin
// main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
And test dependencies in the module:
apply plugin: 'kotlin-android'
dependencies {
androidTestCompile deps.kotlin
androidTestCompile deps.kotlinTest
androidTestCompile deps.kotlinTestJunit
}
Project's build script:
ext.deps = [
kotlin : "org.jetbrains.kotlin:kotlin-stdlib:$ver.kotlin",
kotlinTest : "org.jetbrains.kotlin:kotlin-test:$ver.kotlin",
kotlinTestJunit : "org.jetbrains.kotlin:kotlin-test-junit:$ver.kotlin"
]
I'm using version 1.0.5-2 of Kotlin.
We have a custom test runner as testInstrumentationRunner
which registers some idling resources and some activity hook for unlocking device screen and acquiring the wake lock, and uses TestButler for disabling animations and stabilizing tests. However, the tests are annotated with Espresso jUnit 4 runner (@RunWith(AndroidJUnit4::class)
). But the fact that Android Studio runs the tests without any issues is confusing.
Solution
The kotlin test compile task needs to run after the relevant assemble task for class references to work properly.
For assembleDebugAndroidTest
, make sure assembleDebug
is run first.
From the command line, the order of tasks can be checked quickly with gradle "dry run" like so:
./gradlew assembleDebugAndroidTest -m
If assembleDebug
is not in the correct order. You can fix the order by adding the following to your build file:
afterEvaluate {
compileDebugAndroidTestKotlin.dependsOn assembleDebug
}
Answered By - Oguz Babaoglu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.