https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Aleksey Kornienko

01/31/2019, 5:06 PM
Hello, maybe I do something incorrect, but I can’t run unit test in multiplatform project for Android. For example:
Copy code
class AnalyticsTest {
    private lateinit var context: Context

    @Before
    fun setUp() {
        context = InstrumentationRegistry.getInstrumentation().targetContext
    }

    @After
    fun tearDown() {
    }
}
The error I get
Copy code
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
It doesn’t work only in multiplatform project
my build.gradle.kt
Copy code
plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.multiplatform")
}

group = "com.devtodev"
version = "2.0"

android {
    compileSdkVersion(28)
    defaultConfig {
        minSdkVersion(16)
        targetSdkVersion(28)

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        useLibrary("android.test.runner")
        useLibrary("android.test.base")
        useLibrary("android.test.mock")
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android.txt"), "<http://proguard-rules.pro|proguard-rules.pro>")
        }
    }

    sourceSets {
        val main by getting {
            setRoot("src/androidMain")
        }
        val test by getting {
            setRoot("src/androidTest")
        }
    }

    packagingOptions {
        exclude("META-INF/*.kotlin_module")
    }
}

kotlin {
    android {
        
    }

    iosX64 {
        binaries {
            framework {
                embedBitcode("disable")
            }
        }
    }
    iosArm32 {
        binaries.framework()
    }
    iosArm64 {
        binaries.framework()
    }

    sourceSets {
        fun dependants(name: String, targets: Set<String>) {
            val main = maybeCreate("${name}Main")
            val test = maybeCreate("${name}Test")
            for (target in targets) {
                maybeCreate("${target}Main").dependsOn(main)
                maybeCreate("${target}Test").dependsOn(test)
            }
        }

        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }

        val androidMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib")
            }
        }

        val androidTest by getting {

        }

        val ios = setOf("iosX64", "iosArm32", "iosArm64")
        dependants("ios", ios)
    }
}


dependencies {
    androidTestImplementation("junit:junit:4.12")
    androidTestImplementation("org.jetbrains.kotlin:kotlin-test")
    androidTestImplementation("org.jetbrains.kotlin:kotlin-test-junit")

    androidTestImplementation("androidx.test:runner:1.1.1")
    androidTestImplementation("androidx.test:rules:1.1.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.1.1")
}
I think the reason is because kotlin directory don’t marked as Android JUnit
y

yshrsmz

02/01/2019, 1:26 AM
I think you need robolectric
@Aleksey Kornienko
a

Aleksey Kornienko

02/01/2019, 9:14 AM
Yes, I hear about this framework, but at the moment I want to understand and solve the problem with Android JUnit 🙂
y

yshrsmz

02/01/2019, 9:30 AM
I'm not sure if
androidTest
in mpp module means
androidTest
in ordinary android module. But I can run
androidTest
with android classes if I use Robolectric, so I assume
androidTest
in mpp module runs on jvm, thus we need Robolectric if we want to use that
InstrumentationRegistry
a

Aleksey Kornienko

02/01/2019, 10:16 AM
Thanks for your reply! If it is really impossible at the moment robolectric can be a good variant.
r

ribesg

02/01/2019, 10:26 AM
Are you sure your project is aware that you have sources in /kotlin ? The android plugin only expects /java by default. Or maybe I missed something
a

Aleksey Kornienko

02/01/2019, 10:40 AM
Maybe you’r right, but I added this to my build.gradle.kts file and still have no effect 😞
Copy code
sourceSets {
        val main by getting {
            setRoot("src/androidMain")
        }
        val test by getting {
            setRoot("src/androidTest")
        }

        getByName("androidTest").java.srcDirs("src/androidTest/kotlin")
    }
Haha. Finally (after invalidate cache/restart) my test start work. Thank’s to all for help 🙂
r

ribesg

02/01/2019, 11:05 AM
The good old “turn it off and on again”
69 Views