1

我一直在尝试 Kotlin 并实现一个应用程序。到目前为止,运行该应用程序运行良好,但我无法运行单元测试。

安卓工作室:2.3.3

这是我的 Gradle 设置:

build.gradle(项目)

buildscript {
    ext.kotlin_version = '1.1.3'
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:3.5.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(模块)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    kapt {
        generateStubs = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile group: 'com.google.code.gson', name: 'gson', version:'2.8.0'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.3.2'
}
repositories {
    mavenCentral()
}


class FileUtils(assets: AssetManager) {
    val assets = assets

    fun <T> readJsonFromAsset(fileName: String, tClass: Class<T>): T {
        val text: String? = this.assets.open(fileName)?.bufferedReader()?.readText();

        val typeToken = object : TypeToken<T>() {}.type
        val gson: Gson = Gson()
        return gson.fromJson<T>(text, typeToken)
    }
}



@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)

    class FileUtilsTest {

        val fileUtils: FileUtils

        init {
            val assetManager = Robolectric.buildActivity(Activity::class.java).create().get().assets
            fileUtils = FileUtils(assetManager)
        }


        @Test
        fun readJsonFromAsset_ShouldGetQuestionList() {
            val questions = fileUtils.readJsonFromAsset("part1.json", Questions::class.java)
            assert(7 == questions.size)
        }

        class Questions : ArrayList<Question>()
    }

错误信息:

Information:Gradle tasks [:app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:compileDebugUnitTestSources]
    Error:Execution failed for task ':app:kaptDebugKotlin'.
    > Internal compiler error. See log for more details

我试过的:

Tried with kotlin_version from 1.1.1 to 1.1.3
Tried with compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
Clean Project
Clean cache and restart Android Studio

我认为 Kotlin 不能顺利地与某些库一起使用,并且需要努力修复它。如果我不能使单元测试比使用 Kotlin 的 Java 更容易,我不想使用 Kotlin 但想进行更多实验。我会感谢你的帮助。

4

0 回答 0