2017-09-29 8 views
1

Androidスタジオプロジェクト内のKotlinモジュールのテストパッケージ内のメインクラスにアクセスできないようです。以下に示すコードはすべて、AndroidアプリにインポートされるKotlin JVMモジュール内にあることに注意してください。テストパッケージがメインパッケージに定義されたKotlinクラスを読み取らない

ここに私のsrc/main/javaコードです:src/test/java

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) 

私のテストコード:

class CreateUser { 

    @Test 
    fun createRandomUser() { 
     val random = Random() 
     val randomNumber = random.nextInt(10000000) 
     val customer = Customer("password", "password", "lastName", "firstName", "[email protected]") 

    } 
} 

マイbuild.gradleコードは次のようになります。

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

ルートbuild.gradleファイルとして見えますが、次のとおりです:

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

私が手にエラーがGradleのは、テストクラスでCustomerUnresolved reference: Customer)を解決することができないということです。主なクラスはテストソースディレクトリには含まれていないようです。それでも、IDEで解決されます。

+0

'src/main/kotlin'と' src/test/kotlin'にそれぞれ定義してみませんか? – Opal

+0

それは違いますか? @Opal – blackpanther

+0

グラデルの視点から見るとよいかもしれません。 GradleはJavaコンパイラを使用してJavaファイルをコンパイルし、kotlinコンパイラを使用してkotlinファイルをコンパイルします。 kotlinファイルが誤って配置されていると、それらはコンパイルのために選択されない可能性があります。 – Opal

答えて

0

私は解決策を見つけました。私のbuild.gradleに明示的にsrcフォルダを指定し、すべてのKotlinコードをそれぞれsrc/main/kotlinsrc/test/kotlinに入れなければならないようです。

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'] 
} 

私はテストが期待どおりに動作するために始めた、ということでしたら - レポートがさえ偉大であるジェンキンス上で生成されています。

関連する問題