1

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

Geert Berkers
  • 653
  • 7
  • 19
  • try after changing this line: proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), file("proguard-rules.pro")) – Shweta Chauhan Mar 30 '19 at 08:56
  • I commented the line, but that didn''t help. Any other options? – Geert Berkers Mar 30 '19 at 11:45
  • 1
    no, don't comment it replace it with my line and also want to ask you changed setting.gradle to setting.gradle.kts right ? of cource you did that but just for confirmaton. – Shweta Chauhan Mar 30 '19 at 13:00
  • I replaced it, but i still get the same error. Yes, everything converted to .gradle.kts. maybe an option is to use kotlin dsl to run groovy dsl for this specific test? – Geert Berkers Apr 01 '19 at 07:24

2 Answers2

2

The following works:

  testOptions {
    unitTests.apply {
      isReturnDefaultValues = true
    }
  }

Note that you need to do a gradle sync to pick up the configuration

Tobrun
  • 18,291
  • 10
  • 66
  • 81
0

I finally managed to find a solution using:

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

which contains:

android {
    testOptions {
        unitTests.returnDefaultValues = true
    }
}
Geert Berkers
  • 653
  • 7
  • 19