Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share code between unit test and instrumentation tests when using kotlin

Similar question: Sharing code between Android Instrumentation Tests and Unit Tests in Android Studio

My setup is the following:

  • src/test folder that contains unit tests. These can be either Java or Kotlin classes
  • src/androidTest that contains instrumentation tests. These can also be either Java or Kotlin classes
  • src/sharedTest is a folder that contains a bunch of utils that are shared between unit and instrumentation tests.

This sharing is defined in gradle as:

sourceSets {
    test.java.srcDirs += 'src/sharedTest/java'
    androidTest.java.srcDirs += 'src/sharedTest/java'
}

This allows any Java class in src/test or src/androidTest to access the utils. but not the Kotlin unit tests. My assumption is that they are not added to the sourceSets.

My question is: how can I add them? I tried:

sourceSets {
    test.kotlin.srcDirs += 'src/sharedTest/java'
}

But that doesn't seem to work.

like image 858
verybadalloc Avatar asked Nov 08 '22 20:11

verybadalloc


1 Answers

The default setup would be making the Kotlin source sets visible to the Java compiler and the IDE as well:

android {
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        test.java.srcDirs += 'src/sharedTest/java'
        androidTest.java.srcDirs += 'src/sharedTest/java'
    }
}

You don't need to configure the Kotlin source sets by itself.

like image 61
tynn Avatar answered Nov 15 '22 07:11

tynn