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

Willis

01/08/2019, 12:22 PM
Anyone here used mockK to test their common code in multiplatform project? I’m having an
Unresolved reference: mockK
error when building/executing the test even if InteliJ autocompletion finds the library without any problem 😕
s

Sabrina Namur

01/08/2019, 12:59 PM
I'm using mockK. What platforms do you targeting? And can you show your gradle file?
j

Jonas Bark

01/08/2019, 1:08 PM
implementation "io.mockkmockk common$mockk_version" implementation "io.mockkmockk$mockk_version" did you include both in commonTest? did you include io.mockkmockk$mockk_version for the jvmTest source set?
w

Willis

01/08/2019, 2:47 PM
Thanks a lot for the help @Sabrina Namurand @Jonas Bark I feel I’m making progress but it’s still not running the test. By adding the mockk:mockk dep to the commonMain now I gets this error:
Conflict with dependency 'org.jetbrains.kotlin:kotlin-reflect' in project ':app'. Resolved versions for app (1.3.11) and test app (1.3.0) differ. See <https://d.android.com/r/tools/test-apk-dependency-conflicts.html> for details.
Would Mockk not compatible with Kotlin 1.3.11?
Here is my gradle file
Copy code
plugins {
   id 'org.jetbrains.kotlin.multiplatform' version '1.3.11'
}

repositories {
   google()
   jcenter()
   mavenCentral()

   maven { url "<https://dl.bintray.com/kotlin/ktor>" }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-multiplatform'

android {
   compileSdkVersion 28
   defaultConfig {
       applicationId "org.jetbrains.kotlin.mpp_app_android"
       minSdkVersion 15
       targetSdkVersion 28
       versionCode 1
       versionName "1.0"
       testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
       release {
           minifyEnabled false
       }
   }
}

dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'

}

kotlin {
   targets {
       fromPreset(presets.android, 'android')
       // This preset is for iPhone emulator
       // Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
       fromPreset(presets.iosX64, 'ios') {
           compilations.main.outputKinds('FRAMEWORK')
       }
   }
   sourceSets {
       commonMain {
           dependencies {
               implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.3.11'
               implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.1.0'
               implementation 'io.ktor:ktor-client-core:1.1.1'

           }
       }
       commonTest {
           dependencies {
               implementation 'org.jetbrains.kotlin:kotlin-test-common:1.3.11'
               implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.11'
               implementation "io.mockk:mockk-common:1.9"
               implementation "io.mockk:mockk:1.9"
           }
       }
       androidMain {
           dependencies {
               implementation 'org.jetbrains.kotlin:kotlin-stdlib'
               implementation "io.ktor:ktor-client-android:1.1.1"
               implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'
           }
       }
       androidTest {
           dependencies {
               implementation 'org.jetbrains.kotlin:kotlin-test'
               implementation 'org.jetbrains.kotlin:kotlin-test-junit'
           }
       }
       iosMain {
           dependencies{
               implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.1.0'
               implementation 'io.ktor:ktor-client-core-native:1.1.1'
               implementation 'io.ktor:ktor-client-ios:1.1.1'
           }
       }
       iosTest {
       }
   }
}
j

Jonas Bark

01/08/2019, 2:49 PM
you'll also need to add io.mockk:mockk to androidTest I think this probably won't fix the error you mentioned though
try moving both 'org.jetbrains.kotlin:kotlin-test' to the android dependencies part and use testImplementation instead of implementation
w

Willis

01/08/2019, 3:00 PM
Thanks, I’ve added
Copy code
configurations.all {
       resolutionStrategy {
           forcedModules = [
                   "org.jetbrains.kotlin:kotlin-reflect:1.3.11"
           ]
       }
   }
To the build gradle as well and now it’s working 🎉
I’m thankful for your help, it’s always tricky without clear documentation 🙂
148 Views