Search code examples
gradlekotlingradle-kotlin-dslkotlin-dsl

Kotlin Gradle DSL JSONObject not mocked


Since I converted my Groovy to Kotlin DSL gradle, my unittests are not working anymore. I get the error:

java.lang.RuntimeException: Method get in org.json.JSONObject not mocked. See http://g.co/androidstudio/not-mocked for details.

So i followed the link and added testoptions to all my build.gradle.kts files. But after this it still doesn't work.

My (builsSrc) build.gradle.kts file:

plugins {
    `kotlin-dsl`
}

repositories {
    google()
    jcenter()
}

My (App) build.gradle.kts file:

plugins {
    id("com.android.library")
    kotlin("android")
    kotlin("android.extensions")
}


android {
    compileSdkVersion(Versions.Android.compileSdkVersion)

    defaultConfig {
        versionCode = Versions.Android.appVersionCode
        versionName = Versions.Android.appVersionName

        minSdkVersion(Versions.Android.minSdkVersion)
        targetSdkVersion(Versions.Android.targetSdkVersion)

        testInstrumentationRunner = Config.Test.instrumentationRunner
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }

    testOptions {
        unitTests.setReturnDefaultValues(true)
    }
}

dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))

    implementation(Depends.Kotlin.reflect)
    implementation(Depends.Kotlin.kotlinStdLib)

    testImplementation(Depends.TestLibraries.json)
    testImplementation(Depends.TestLibraries.jUnit)
    androidTestImplementation(Depends.TestLibraries.jUnitRunner)
    androidTestImplementation(Depends.TestLibraries.espressoCore)
}

It doens't look for me that I'm missing something. Does anyone have an id on how to fix this?

Image Method not mocked


Solution

  • I finally managed to find a solution using:

    apply(from = "../testOptions.gradle")
    

    which contains:

    android {
        testOptions {
            unitTests.returnDefaultValues = true
        }
    }