Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Multiplatform Project Unit Test Issue

Today I just updated my Android Studio to

Android Studio 3.5.2
Build #AI-191.8026.42.35.5977832, built on October 31, 2019
JRE: 1.8.0_202-release-1483-b49-5587405 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.1

I updated the Android Studio because it prompts error to me when the kotlin version is below 1.3.60.

However I am facing problem with the unit test file. Previously I can run the unit test without any problem. Now, when I click the run button besides the function name (on the left), it shows Nothing Here.

enter image description here

Below is all my dependencies

// build.gradle

buildscript {
ext.kotlin_version = '1.3.60'
repositories {
    google()
    jcenter()
    maven { url "https://kotlin.bintray.com/kotlinx" }
}

dependencies {
    // classpath 'com.android.tools.build:gradle:3.4.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}

allprojects {
  repositories {
    google()
    jcenter()
  }
}

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


 apply plugin: 'kotlin-multiplatform'
 apply plugin: 'kotlinx-serialization'

kotlin {
targets {
    final def iOSTarget = presets.iosX64 // Simulator

    fromPreset(iOSTarget, 'iOS') {
        binaries {
            framework('SharedCode')
        }
    }
    fromPreset(presets.jvm, 'android')
}

sourceSets {
    commonMain {
        dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib-common'
            implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
        }
    }

    iOSMain {
        dependsOn commonMain
    }

    androidMain.dependencies {
        api 'org.jetbrains.kotlin:kotlin-stdlib'
    }

    commonTest {
        dependsOn commonMain
    }
}
}

Is there something that I missed out in this new Android Studio and kotlin version. I have been struggling with this for hours. Browsed web but seems couldn't find any help.

Thanks

like image 665
alvinWB Avatar asked Nov 26 '19 10:11

alvinWB


People also ask

Is kotlin multiplatform stable?

Since Kotlin Multiplatform Mobile is now in Beta, we are working on stabilizing the corresponding plugin for Android Studio and will be regularly releasing new versions that include new features, improvements, and bug fixes.

What is Robolectric?

Robolectric is a framework that allows you to write unit tests and run them on a desktop JVM while still using Android API. Robolectric provides a JVM compliant version of the android. jar file.

How do you do unit testing on Kotlin?

Each test should be created from the following blocks: Arrange/Given - in which we will prepare all needed data required to perform test. Act/When - in which we will call single method on tested object. Assert/Then - in which we will check result of the test, either pass or fail.

Is kotlin multiplatform native?

With Kotlin Multiplatform, you can create different multiplatform projects for multiple platforms, including web, desktop, and other native platforms. Kotlin applications will work on different operating systems, such as macOS, Windows, Linux, Android, iOS, watchOS, and others.


1 Answers

In my case, this issues happened when I updated from

  • Android Studio 3.5.3 and Kotlin plugin version v1.3.50-release-Studio3.5-1

to

  • Android Studio 3.6.3 with Kotlin plugin 1.3.71-release-Studio3.6-1

All of my common Kotlin MPP tests were not running from the expected IDE side function "green" run and displayed the Nothing here message too.

I tried a few things, one of them being a full uninstall and then install but nothing seemed to work.

What ended up working for me was (with some caveats):

  • created a new test (works with existing tests too, but just as an example)

pic 1

to make sure the Nothing Here shows.

Then:

  • open the dropdown next to the Emulator selected one and click Edit Configurations
  • under Android JUnit click the + button to add a new configuration
  • it should look like this:

pic2

make sure the test kind is Class and that you have the correct path to the file. For example:

com.my.path.is.correct.MyNewTestCase

Also, make sure you have the correct module for the common Kotlin; in my case it was common (not commonTest).

After the above you should be able to run the test class from the main Run button (again, next to the Emulator drop down list) and even run it as Debug.

The "green" run button next to the function will still say Nothing here but adding a breakpoint and running the new configuration from the main Debug button does work for me:

enter image description here

This is obviously not perfect, but it was the only way to have it working from the IDE that I could find.

Hope it helps!

like image 166
Guimo Avatar answered Oct 16 '22 15:10

Guimo