Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test package does not read Kotlin classes defined in main package

I cannot seem to access main classes within the test package in my Kotlin module within an Android Studio project. Please note that all code shown below is within a Kotlin JVM module that is imported into the Android app.

Here's 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)

My test 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 the following:

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 as follows:

// 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 cannot resolve Customer (Unresolved reference: Customer) in the Test class. It doesn't seem to include main classes into the test source directory. Yet, it resolves in the IDE.

like image 755
blackpanther Avatar asked Sep 29 '17 13:09

blackpanther


1 Answers

Ok, I have found the solution. It seems I have to specify the src folders explicitly in my build.gradle and put all 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 I did that, the tests started to work as expected - reports are even generated on Jenkins which is great.

like image 52
blackpanther Avatar answered Oct 14 '22 01:10

blackpanther