The test package does not read the Kotlin classes defined in the main package
I can’t seem to have access to the main classes in the test package in my Kotlin module in the Android Studio project. Note that all the code shown below is in the Kotlin JVM module imported into the Android application.
Here is my src/main/java
code:
import com.google.gson.annotations.SerializedName
data class Customer(val password1: String,
val password2: String,
@SerializedName("last_name") val lastName: String,
@SerializedName("first_name") val firstName: String,
val email: String)
I tested the code in src/test/java
:
class CreateUser {
@Test
fun createRandomUser() {
val random = Random()
val randomNumber = random.nextInt(10000000)
val customer = Customer("password", "password", "lastName", "firstName", "[email protected]")
}
}
My build.gradle
code looks like this:
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'kotlin'
repositories {
mavenCentral()
jcenter()
}
dependencies {
some other compile dependencies
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
testCompile "org.hamcrest:hamcrest-all:1.3"
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
archives javadocJar
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
The
root build.gradle
file looks like this:
// Top-level build file where you can add configuration options
common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
NOTE: Do not place your application dependencies here; they belong
in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
credentials { username authToken }
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
versionName = "0.1.1"
rxJavaVersion = "2.1.3"
okHttpVersion = "3.9.0"
retrofitVersion = "2.3.0"
rxJava2AdapterVersion = "1.0.0"
googleGsonVersion = "2.8.0"
}
The error I get is that gradle can’t resolve Customer (Unresolved reference: Customer)
in the Test class. It doesn’t seem to include the main class into the test source directory. However, it is resolved in the IDE.
Solution
Ok, I found the solution. It looks like I have to explicitly specify the src folder in my build.gradle and put all the Kotlin code in src/main/kotlin and src/test/kotlin
respectively.
sourceSets {
main.kotlin.srcDirs = ['src/main/kotlin', 'src/main/java']
main.java.srcDirs = []
test.kotlin.srcDirs = ['src/test/kotlin', 'src/test/java']
test.java.srcDirs = ['src/test/kotlin', 'src/test/java']
}
Once done, the tests started working as expected – even generating reports on Jenkins, which was great.