Best Mockito-kotlin code snippet using org.mockito.kotlin.Mockito
Vers.kt
Source:Vers.kt
1@file:Suppress("PackageDirectoryMismatch", "unused", "MemberVisibilityCanBePrivate")2package pl.mareklangiewicz.deps3import libs4import pl.mareklangiewicz.defaults.v5/**6 * Common dependencies versions for java/kotlin/android projects7 *8 * @see <a href="https://github.com/langara/deps.kt">https://github.com/langara/deps.kt</a>9 */10object Vers {11 val kotlin14 = v(1, 4, 32)12 val kotlin15 = v(1, 5, 31)13 val kotlin16 = v(1, 6, 21)14 val kotlin = kotlin1615 // https://kotlinlang.org/docs/releases.html#release-details16 // https://github.com/JetBrains/kotlin/blob/master/ChangeLog.md17 // https://github.com/JetBrains/kotlin/releases18 const val defaultJvm = "11" // I had terrible issues with "16" (andro compose project)19 const val kotlinxDateTime = "0.3.2"20 // https://github.com/Kotlin/kotlinx-datetime21 const val kotlinxCoroutines = "1.6.0"22 // https://github.com/Kotlin/kotlinx.coroutines/releases23 const val kotlinxSerialization = "1.3.2"24 // https://github.com/Kotlin/kotlinx.serialization/releases25 const val kotlinxAtomicFu = "0.17.1"26 // https://github.com/Kotlin/kotlinx.atomicfu/releases27 const val kotlinxHtml = "0.7.5"28 // https://github.com/Kotlin/kotlinx.html/releases29 const val kotlinxNodeJs = "0.0.7"30 // https://github.com/Kotlin/kotlinx-nodejs31 // just a reference - not useful in typical cases32 const val gradle5 = "5.6.4"33 const val gradle6 = "6.8.3"34 const val gradle7 = "7.4.2"35 const val gradle = gradle736 // https://gradle.org/releases/37 // https://services.gradle.org/versions38 // https://services.gradle.org/versions/current39 // https://services.gradle.org/versions/release-candidate40 const val composeJbMain = "1.1.1"41 const val composeJbEdge = "1.2.0-alpha01-dev686"42 const val composeJb = composeJbEdge43 // https://github.com/JetBrains/compose-jb44 // https://github.com/JetBrains/compose-jb/releases45 // https://github.com/JetBrains/compose-jb/blob/master/CHANGELOG.md46 const val composeAndroid = "1.2.0-beta02"47 const val composeAndroidCompiler = composeAndroid48 const val composeAndroidMaterial3 = "1.0.0-alpha12"49 // https://developer.android.com/jetpack/androidx/releases/compose50 const val googleAccompanist = "0.24.7-alpha"51 const val googleAccompanistImage = "0.15.0"52 const val googleAccompanistPicasso = "0.6.2"53 // https://search.maven.org/search?q=g:com.google.accompanist54 // https://google.github.io/accompanist/55 const val androidGradlePlugin = "7.4.0-alpha02"56 // https://maven.google.com/web/index.html#com.android.tools.build:gradle57 // https://developer.android.com/studio/releases/gradle-plugin58 // https://google.github.io/android-gradle-dsl/59 @Deprecated("Use https://developer.android.com/studio/build/maven-publish-plugin")60 const val androidMavenGradlePlugin = "2.1" // https://github.com/dcendents/android-maven-gradle-plugin/releases61 const val nexusPublishGradlePlugin = "1.1.0" // https://github.com/gradle-nexus/publish-plugin/62 val dokkaGradlePlugin = kotlin // will it be synced with kotlin version in the future?63 const val androidSdkCompile = 3164 const val androidSdkTarget = androidSdkCompile65 const val androidSdkMin = 2666 // https://developer.android.com/about/dashboards/index.html67 // https://source.android.com/setup/start/build-numbers68 @Deprecated("Deprecated with android gradle plugin 3.0.0 or higher")69 const val androidBuildTools = "30.0.2"70 // https://developer.android.com/studio/releases/build-tools.html71 @Deprecated("Use androidx")72 const val androidSupport = "28.0.0"73 // https://developer.android.com/topic/libraries/support-library/revisions.html74 // https://developer.android.com/jetpack/androidx/versions75 // https://dl.google.com/dl/android/maven2/index.html76 const val androidxCore = "1.7.0-rc01"77 const val androidxActivity = "1.4.0-rc01"78 const val androidxAppcompat = "1.4.0-beta01"79 const val androidxConstraint1 = "1.1.3"80 // https://developer.android.com/training/constraint-layout81 const val androidxConstraint2 = "2.1.3"82 const val androidxConstraint = androidxConstraint283 const val androidxNavigation = "2.5.0-rc01"84 // https://developer.android.com/jetpack/androidx/releases/navigation85 const val androidxRecyclerview = "1.3.0-alpha01"86 const val androidxCardview = "1.0.0"87 const val androidMaterial = "1.5.0-alpha04"88 const val androidxAnnotation = "1.3.0-beta01"89 const val androidxPreference = "1.1.1"90 const val androidxBrowser = "1.4.0-rc01"91 const val androidxBrowserHelper = "2.3.0"92 // https://github.com/GoogleChrome/android-browser-helper93 const val androidxPercentLayout = "1.0.0"94 const val androidxFlexboxLayout = "3.0.0"95 // https://github.com/google/flexbox-layout/releases96 const val androidxLifecycle = "2.5.0-rc01"97 // https://developer.android.com/jetpack/androidx/releases/lifecycle98 const val androidxCamera = "1.1.0-rc01"99 const val androidxCameraExtensions = "1.0.0-alpha32"100 const val androidxCameraView = "1.0.0-alpha32"101 // https://developer.android.com/jetpack/androidx/releases/camera102 const val androidxRoom = "2.5.0-alpha01"103 // https://developer.android.com/jetpack/androidx/releases/room104 const val androidxTest = "1.4.1-alpha06"105 const val androidxTestRunner = "1.5.0-alpha03"106 const val androidxTestRules = androidxTest107 const val androidxTestExtTruth = "1.5.0-alpha06"108 const val androidxTestExtJUnit = "1.1.4-alpha06"109 // https://developer.android.com/jetpack/androidx/releases/test110 const val androidxEspresso = "3.5.0-alpha06"111 const val androidCommons = "0.0.24"112 // https://github.com/elpassion/android-commons/releases113 const val rxjava2 = "2.2.16"114 const val rxjava3 = "3.1.4"115 // https://github.com/ReactiveX/RxJava/releases116 const val rxkotlin = "3.0.1"117 // https://github.com/ReactiveX/RxKotlin/releases118 const val rxbinding = "4.0.0"119 // https://github.com/JakeWharton/RxBinding120 // https://github.com/JakeWharton/RxBinding/releases121 // https://github.com/JakeWharton/RxBinding/blob/master/CHANGELOG.md122 const val rxrelay = "3.0.1"123 // https://github.com/JakeWharton/RxRelay124 // https://github.com/JakeWharton/RxRelay/tags125 const val rxandroid = "3.0.0"126 // https://github.com/ReactiveX/RxAndroid/releases127 const val rxlifecycle = "4.0.2"128 // https://github.com/trello/RxLifecycle/releases129 const val retrofit = "2.9.0"130 // https://github.com/square/retrofit131 // https://github.com/square/retrofit/tags132 const val okhttp = "5.0.0-alpha.7"133 // https://github.com/square/okhttp134 // https://github.com/square/okhttp/tags135 const val okio = "3.1.0"136 // https://square.github.io/okio/changelog/137 // https://square.github.io/okio/#releases138 const val dbusJava = "3.3.1"139 // https://github.com/hypfvieh/dbus-java140 const val dbusKotlin = "0.0.08"141 // https://github.com/langara/dbus-kotlin142 const val javaWebsocket = "1.5.2"143 // https://mvnrepository.com/artifact/org.java-websocket/Java-WebSocket144 // const val slf4jSimple = "1.7.30"145 const val slf4jSimple = "2.0.0-alpha7"146 // https://mvnrepository.com/artifact/org.slf4j/slf4j-simple147 const val log4j2 = "2.17.2"148 // http://logging.apache.org/log4j/2.x/maven-artifacts.html149 const val googleServicesPlugin = "4.3.10"150 // https://developers.google.com/android/guides/google-services-plugin151 const val googlePlayServicesBase = "18.0.0"152 // https://developers.google.com/android/guides/setup153 // https://developers.google.com/android/guides/releases154 const val firebaseGitlive = "1.4.3"155 // https://github.com/GitLiveApp/firebase-kotlin-sdk156 const val firebaseCrashlyticsPlugin = "2.8.1"157 // https://firebase.google.com/docs/crashlytics/get-started?platform=android158 const val firebaseAdmin = "8.1.0"159 // https://firebase.google.com/docs/admin/setup160 const val firebaseAndroidBoM = "29.0.2"161 // https://firebase.google.com/support/release-notes/android162 // https://firebase.google.com/docs/android/setup#add-sdks163 const val firebaseUiAuth = "8.0.0"164 // https://github.com/firebase/FirebaseUI-Android165 // https://github.com/firebase/FirebaseUI-Android/releases166 // https://firebase.google.com/docs/auth/android/firebaseui167 const val googleCloudBoM = "24.0.0"168 // https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM169 const val googleAuth = "1.3.0"170 // https://github.com/googleapis/google-auth-library-java171 const val googleGuavaJre = "31.0.1-jre"172 const val googleGuavaAndroid = "31.0.1-android"173 // https://github.com/google/guava174 const val googleGuavaMissingMetadataPlugin = "0.5"175 // https://github.com/jjohannes/missing-metadata-guava176 const val picasso = "2.8"177 // https://github.com/square/picasso178 // https://github.com/square/picasso/releases179 const val materialDialogs = "3.3.0"180 // https://github.com/afollestad/material-dialogs181 // https://github.com/afollestad/material-dialogs/releases182 const val leakcanary = "2.7"183 // https://github.com/square/leakcanary/releases184 const val paperwork = "1.2.7"185 // https://github.com/zsoltk/paperwork/releases186 const val mockitoCore2 = "2.28.2"187 const val mockitoCore3 = "3.12.4"188 const val mockitoCore4 = "4.1.0"189 const val mockitoCore = mockitoCore4190 // https://github.com/mockito/mockito/releases191 // https://search.maven.org/artifact/org.mockito/mockito-core192 const val mockitoKotlin2 = "2.2.11"193 const val mockitoKotlin3 = "3.2.0"194 const val mockitoKotlin4 = "4.0.0"195 const val mockitoKotlin = mockitoKotlin4196 // https://github.com/nhaarman/mockito-kotlin/releases197 const val mockitoAndroid2 = "2.28.2"198 const val mockitoAndroid3 = "3.12.4"199 const val mockitoAndroid4 = "4.1.0"200 const val mockitoAndroid = mockitoAndroid4201 // https://search.maven.org/artifact/org.mockito/mockito-android202 const val junit4 = "4.13.2"203 // https://github.com/junit-team/junit4/releases204 const val junit5 = "5.8.2"205 // https://github.com/junit-team/junit5/releases206 const val googleTruth = "1.1.3"207 // https://github.com/google/truth/releases208 const val androidTestRunnerClass = "androidx.test.runner.AndroidJUnitRunner"209 // https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html210 const val realm = "10.9.0"211 // https://docs.mongodb.com/realm/sdk/android/install/212 const val ktor = "1.6.7"213 // https://maven.pkg.jetbrains.space/public/p/ktor/eap/io/ktor/ktor/214 // https://github.com/ktorio/ktor215 // https://github.com/ktorio/ktor/releases216 const val rsocket = "0.14.3"217 // https://github.com/rsocket/rsocket-kotlin218 // https://github.com/rsocket/rsocket-kotlin/releases219 const val splitties = "3.0.0"220 // https://github.com/LouisCAD/Splitties/releases221 // https://github.com/LouisCAD/Splitties222 const val docoptJava = "0.6.0.20150202"223 // https://mvnrepository.com/artifact/com.offbytwo/docopt224 // https://github.com/docopt/docopt.java225 const val npmReact = "18.0.0"226 // https://reactjs.org/versions227 const val npmStyled = "5.3.3"228 // https://github.com/JetBrains/kotlin-wrappers229 // https://mvnrepository.com/artifact/org.jetbrains.kotlin-wrappers/kotlin-wrappers-bom230 val kotlinJsWrappersBoM = "0.0.1-pre.330-kotlin-$kotlin"231 // https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-react/README.md232 // https://mvnrepository.com/artifact/org.jetbrains.kotlin-wrappers/kotlin-react233 @Deprecated("Use BoM")234 val kotlinJsWrappersReact = "$npmReact-pre.330-kotlin-$kotlin"235 // https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-react-dom/README.md236 // https://mvnrepository.com/artifact/org.jetbrains.kotlin-wrappers/kotlin-react-dom237 @Suppress("DEPRECATION")238 @Deprecated("Use BoM")239 val kotlinJsWrappersReactDom = kotlinJsWrappersReact240 // https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-styled/README.md241 // https://mvnrepository.com/artifact/org.jetbrains.kotlin-wrappers/kotlin-styled242 @Deprecated("Use BoM")243 val kotlinJsWrappersStyled = "$npmStyled-pre.330-kotlin-$kotlin"244 // My libs - see details in LibsDetails.kt:245 // https://github.com/langara/deps.kt/blob/master/src/main/kotlin/deps/LibsDetails.kt246 // https://repo1.maven.org/maven2/pl/mareklangiewicz/247 val smokk = libs.SMokK248 val rxmock = libs.RxMock249 val abcdk = libs.AbcdK.version250 val tuplek = libs.TupleK.version251 val uspek = libs.USpek.version252 val upue = libs.UPue.version253 val kommandLine = libs.KommandLine.version254 val templateMPP = libs.TemplateMPP.version255 const val sandboxui = "0.0.5"256 // https://github.com/langara/sandboxui/releases257 const val recyclerui = "0.0.2"258 // https://github.com/langara/recyclerui/releases259}...
Libs.kt
Source:Libs.kt
1object Libs {2 const val gradle_plugin = "com.android.tools.build:gradle:${Versions.gradle_plugin}"3 const val kotlin_plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"4 const val kotlin = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}"5 //support6 const val androidx_appcompat = "androidx.appcompat:appcompat:${Versions.androidx_appcompat}"7 const val androidx_core = "androidx.core:core:${Versions.androidx_core}"8 const val androidx_constraint_layout = "androidx.constraintlayout:constraintlayout:${Versions.androidx_constraint_layout}"9 const val androidx_fragment = "androidx.fragment:fragment-ktx:${Versions.androidx_fragment}"10 const val androidx_activity = "androidx.activity:activity-ktx:${Versions.androidx_activity}"11 const val androidx_viewmodel = "androidx.lifecycle:lifecycle-viewmodel-ktx:${Versions.androidx_lifecycle}"12 const val androidx_livedata = "androidx.lifecycle:lifecycle-livedata:${Versions.androidx_lifecycle}"13 const val material_design = "com.google.android.material:material:${Versions.material_design}"14 const val nav_component_fragment = "androidx.navigation:navigation-fragment-ktx:${Versions.nav_component}"15 const val nav_component_ui = "androidx.navigation:navigation-ui-ktx:${Versions.nav_component}"16 const val work_manager = "androidx.work:work-runtime-ktx:${Versions.work_manager}"17 // Room18 const val room = "androidx.room:room-runtime:${Versions.room}"19 const val room_compiler = "androidx.room:room-compiler:${Versions.room}"20 const val room_ktx = "androidx.room:room-ktx:${Versions.room}"21 const val room_rx = "androidx.room:room-rxjava2:${Versions.room}"22 //retrofit23 const val retrofit = "com.squareup.retrofit2:retrofit:${Versions.retrofit}"24 const val retrofit_rxjava_adapter = "com.squareup.retrofit2:adapter-rxjava:${Versions.retrofit}"25 const val retrofit_rxjava2_adapter = "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"26 const val retrofit_gson_coverter = "com.squareup.retrofit2:converter-gson:${Versions.retrofit}"27 //okhttp28 const val okhttp = "com.squareup.okhttp3:okhttp:${Versions.okhttp}"29 const val okio = "com.squareup.okio:okio:${Versions.okio}"30 //rxjava31 const val rxjava = "io.reactivex.rxjava2:rxjava:${Versions.rxjava}"32 const val rxandroid = "io.reactivex.rxjava2:rxandroid:${Versions.rxandroid}"33 const val rxpreferences = "com.f2prateek.rx.preferences2:rx-preferences:${Versions.rxpreferences}"34 //Firebase35 const val firebase_analytics = "com.google.firebase:firebase-analytics:17.2.3"36 const val firebase_crashlytics = "com.google.firebase:firebase-crashlytics:17.0.0-beta01"37 //dagger38 const val dagger = "com.google.dagger:dagger:${Versions.dagger}"39 const val dagger_support = "com.google.dagger:dagger-android-support:${Versions.dagger}"40 const val dagger_processor = "com.google.dagger:dagger-android-processor:${Versions.dagger}"41 const val dagger_compiler = "com.google.dagger:dagger-compiler:${Versions.dagger}"42 const val coil = "io.coil-kt:coil:0.9.5"43 const val threetenbp = "org.threeten:threetenbp:${Versions.threetenbp}"44 const val gson = "com.google.code.gson:gson:${Versions.gson}"45 const val curl = "com.github.mrmike:ok2curl:${Versions.curl}"46 const val groupie = "com.xwray:groupie:${Versions.groupie}"47 const val groupie_extensions = "com.xwray:groupie-kotlin-android-extensions:${Versions.groupie}"48 const val lottie = "com.airbnb.android:lottie:3.4.0"49 const val joystick = "io.github.controlwear:virtualjoystick:1.10.1"50 const val stateMachine = "com.tinder.statemachine:statemachine:0.2.0"51 // Logging52 const val timber = "com.jakewharton.timber:timber:${Versions.timber}"53 // TEST54 const val junit = "junit:junit:${Versions.junit}"55 const val kotlin_test_runtime = "org.jetbrains.kotlin:kotlin-test:${Versions.kotlin}"56 const val kotlin_test_reflect = "org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlin}"57 const val kotlin_test_annotations = "org.jetbrains.kotlin:kotlin-test-annotations-common:${Versions.kotlin}"58 const val kotlin_test_junit = "org.jetbrains.kotlin:kotlin-test-junit:${Versions.kotlin}"59 const val mockito = "org.mockito:mockito-core:${Versions.mockito}"60 const val mockito_android = "org.mockito:mockito-android:${Versions.mockito}"61 const val mockito_inline = "org.mockito:mockito-inline:${Versions.mockito}"62 const val mockito_kotlin = "com.nhaarman:mockito-kotlin:${Versions.mockito_kotlin}"63 const val androidx_test_runner = "androidx.test:runner:${Versions.androidx_test_runner}"64 const val androidx_test_rules = "androidx.test:rules:${Versions.androidx_test_rules}"65 const val androidx_test_core = "androidx.test:core:${Versions.androidx_test_core}"66 const val androidx_junit = "androidx.test.ext:junit:${Versions.androidx_junit}"67 const val androidx_truth = "androidx.test.ext:truth:${Versions.androidx_truth}"68 const val androidx_espresso_contrib = "androidx.test.espresso:espresso-contrib:${Versions.espresso}"69 const val androidx_espresso_core = "androidx.test.espresso:espresso-core:${Versions.espresso}"70 const val androidx_fragment_testing = "androidx.fragment:fragment-testing:${Versions.fragment_test}"71}...
AndroidFactory.kt
Source:AndroidFactory.kt
...39 }40 /**41 * @param junit [jUnit][LIBRARY_TEST_JUNIT]42 * @param truth [Truth][LIBRARY_TEST_JUNIT]43 * @param mockito [Mockito][LIBRARY_TEST_MOCKITO] & [Mockito Inline][LIBRARY_TEST_MOCKITO_INLINE]44 * */45 fun applyTestDependencies(46 junit: String = "4.+",47 truth: String = "1.+",48 mockito: String = "3.+"49 ) = apply {50 project.dependencies {51 testImplementation(LIBRARY_TEST_JUNIT, junit)52 testImplementation(LIBRARY_TEST_TRUTH, truth)53 testImplementation(LIBRARY_TEST_MOCKITO, mockito)54 testImplementation(LIBRARY_TEST_MOCKITO_INLINE, mockito)55 }56 }57 /** @param version [AndroidX jUnit][LIBRARY_TEST_JUNIT_ANDROID] */...
CountriesRepositoryTest.kt
Source:CountriesRepositoryTest.kt
...15import org.junit.Test16import org.junit.runner.RunWith17import org.mockito.ArgumentMatchers.any18import org.mockito.Mock19import org.mockito.Mockito20import org.mockito.MockitoAnnotations21import retrofit2.Response22@RunWith(AndroidJUnit4::class)23@SmallTest24class CountriesRepositoryTest {25 @Mock26 private val mockApi = Mockito.mock(CountriesApi::class.java)27 @Mock28 private val mockDao = Mockito.mock(CountriesDao::class.java)29 private lateinit var repository: CountriesRepository30 private val country = Country(31 1,32 "Afghanistan",33 listOf(".af"),34 listOf(Currency("AFN", "Afghan afghani", "Ø")),35 listOf("93"),36 "Kabul",37 "Asia",38 "Southern Asia",39 listOf(Language("ps", "pus", "Pashto", "")),40 Translations("Afghanistan", "", "", "", ""),41 "Afghan",42 listOf("IRN", "PAK", "TKM"),43 "",44 652230.0,45 false)46 @Before47 fun setup(){48 MockitoAnnotations.openMocks(this)49 repository = CountriesRepositoryImp(mockApi, ApplicationProvider.getApplicationContext(), mockDao)50 }51 @Test52 fun getAllCountriesFromNetwork() = runBlocking{53 Assert.assertNotNull(mockDao)54 Assert.assertNotNull(mockApi)55 Mockito.`when`(mockDao.getAllCountries()).thenReturn(listOf())56 Mockito.`when`(mockApi.getAllCountries()).thenReturn(Response.success(listOf(country)))57 val result = repository.getAllCountries() as AppResult.Success<List<Country>>58 Assert.assertEquals(1, result.successData.size)59 Assert.assertEquals(country.name, result.successData[0].name)60 }61 @Test62 fun getAllCountriesFromDatabase() = runBlocking {63 Assert.assertNotNull(mockDao)64 Mockito.`when`(mockDao.getAllCountries()).thenReturn(listOf(country))65 val result = repository.getAllCountries() as AppResult.Success<List<Country>>66 Assert.assertEquals(1, result.successData.size)67 Assert.assertEquals(country.name, result.successData[0].name)68 }69 @Test70 fun updateFavourite() = runBlocking{71 Assert.assertNotNull(mockDao)72 Mockito.`when`(mockDao.updateFavourite(any(Int::class.java), any(Boolean::class.java)))73 val result = repository.updateFavourite(1, true) as AppResult.Success<Boolean>74 Assert.assertTrue(result.successData)75 }76 @Test77 fun getFavourites() = runBlocking {78 Assert.assertNotNull(mockDao)79 country.isFavourite = true80 Mockito.`when`(mockDao.getFavourites(any(Boolean::class.java))).thenReturn(listOf(country))81 val result = repository.getFavourites() as AppResult.Success<List<Country>>82 Assert.assertEquals(1, result.successData.size)83 Assert.assertEquals(country.name, result.successData[0].name)84 Assert.assertTrue(result.successData[0].isFavourite)85 }86}...
build.gradle.kts
Source:build.gradle.kts
1plugins {2 val kotlinVersion = "1.5.10"3 java4 kotlin("jvm") version kotlinVersion5}6group = "net.dilius"7version = "1.0-SNAPSHOT"8repositories {9 mavenCentral()10}11val kotlinVersion = "1.5.10"12val coroutinesVersion = "1.5.0"13val junitVersion = "5.8.0-M1"14val mockitoVersion = "3.10.0"15val mockitoKotlinVersion = "3.2.0"16dependencies {17 implementation(kotlin("reflect"))18 implementation(kotlin("stdlib-jdk8"))19 implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", coroutinesVersion)20 implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core-jvm", coroutinesVersion)21 implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8", coroutinesVersion)22 testImplementation(platform("org.junit:junit-bom:$junitVersion"))23 testImplementation("org.junit.jupiter", "junit-jupiter")24 testImplementation("org.junit.jupiter", "junit-jupiter-params")25 testImplementation("org.jetbrains.kotlin", "kotlin-test-junit5", kotlinVersion)26 testImplementation("org.jetbrains.kotlin", "kotlin-test", kotlinVersion)27 testImplementation("org.mockito", "mockito-core", mockitoVersion)28 testImplementation("org.mockito", "mockito-junit-jupiter", mockitoVersion)29 testImplementation("org.mockito.kotlin", "mockito-kotlin", mockitoKotlinVersion)30}31configure<JavaPluginConvention> {32 sourceCompatibility = JavaVersion.VERSION_1133 targetCompatibility = JavaVersion.VERSION_1134}35tasks {36 compileKotlin {37 kotlinOptions {38 freeCompilerArgs = listOf("-Xjsr305=strict")39 jvmTarget = "11"40 }41 }42 compileTestKotlin {43 kotlinOptions {44 freeCompilerArgs = listOf("-Xjsr305=strict")45 jvmTarget = "11"46 }47 }48}...
Dependencies.kt
Source:Dependencies.kt
1object Versions {2 const val kotlin = "1.6.21"3 const val android_x = "1.4.1"4 const val circleindicator = "2.1.6"5 const val junit = "4.13.2"6 const val mockito_core = "4.5.1"7 const val mockito_kotlin = "4.0.0"8 const val truth = "1.1.3"9 const val robolectric = "4.7.3"10 const val gradle_android = "7.1.3"11 const val jacoco = "0.8.8"12 const val min_sdk = 1413 const val target_sdk = 3114 const val compile_sdk = 3115 const val lemniscate_version_code = 20416 const val lemniscate_version_name = "2.0.4"17 const val sample_version_code = 13318 const val sample_version_name = "1.3.3"19}20object Deps {21 const val kotlin_stdlib = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"22 const val appcompat = "androidx.appcompat:appcompat:${Versions.android_x}"23 const val circleindicator = "me.relex:circleindicator:${Versions.circleindicator}"24 const val junit = "junit:junit:${Versions.junit}"25 const val mockito_core = "org.mockito:mockito-core:${Versions.mockito_core}"26 const val mockito_kotlin = "org.mockito.kotlin:mockito-kotlin:${Versions.mockito_kotlin}"27 const val truth = "com.google.truth:truth:${Versions.truth}"28 const val robolectric = "org.robolectric:robolectric:${Versions.robolectric}"29 const val android_gradle_plugin = "com.android.tools.build:gradle:${Versions.gradle_android}"30 const val kotlin_gradle_plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"31}...
TestLibraryDependency.kt
Source:TestLibraryDependency.kt
1private object TestLibraryVersion {2 const val JUNIT = "4.13"3 const val TEST_RUNNER = "1.2.0"4 const val TEST_EXT_RUNNER = "1.1.1"5 const val ESPRESSO_CORE = "3.0.2"6 const val MOCKITO = "3.2.4"7 const val MOCKITO_KOTLIN = "2.2.0"8 const val ANDROID_X_TEST = "2.1.0"9 const val ROBOELECTRIC_VERSION = "4.3"10}11object TestLibraryDependency {12 const val JUNIT = "junit:junit:${TestLibraryVersion.JUNIT}"13 const val TEST_EXT_RUNNER = "androidx.test.ext:junit:${TestLibraryVersion.TEST_EXT_RUNNER}"14 const val TEST_RUNNER = "androidx.test:runner:${TestLibraryVersion.TEST_RUNNER}"15 const val ESPRESSO_CORE = "com.android.support.test.espresso:espresso-core:${TestLibraryVersion.ESPRESSO_CORE}"16 const val MOCKITO_INLINE = "org.mockito:mockito-inline:${TestLibraryVersion.MOCKITO}"17 const val MOCKITO_ANDROID = "org.mockito:mockito-android:${TestLibraryVersion.MOCKITO}"18 const val MOCKITO_KOTLIN = "com.nhaarman.mockitokotlin2:mockito-kotlin:${TestLibraryVersion.MOCKITO_KOTLIN}"19 const val COROUTINES_TEST = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${CoreVersion.COROUTINES_ANDROID}"20 const val ANDROID_X_CORE_TESTING = "android.arch.core:core-testing:${TestLibraryVersion.ANDROID_X_TEST}"21 const val ROBOELECTRIC = "org.robolectric:robolectric:${TestLibraryVersion.ROBOELECTRIC_VERSION}"22}...
mock.gradle.kts
Source:mock.gradle.kts
1package dependencies.test2import Versions3object MockDependencies {4 const val mockitoCore = "org.mockito:mockito-core:${Versions.mockito}"5 const val mockitoJupiter = "org.mockito:mockito-junit-jupiter:${Versions.mockito}"6 const val mockitoInline = "org.mockito:mockito-inline:${Versions.mockito}"7 const val mockitoKotlin = "org.mockito.kotlin:mockito-kotlin:${Versions.mockitoKotlin}"8}9val testImplementation: Configuration by configurations10dependencies {11 testImplementation(MockDependencies.mockitoCore)12 testImplementation(MockDependencies.mockitoJupiter)13 testImplementation(MockDependencies.mockitoInline)14 testImplementation(MockDependencies.mockitoKotlin)15}...
Mockito
Using AI Code Generation
1val mockedList = mock<List<String>>()2whenever(mockedList.get(0)).thenReturn("first")3whenever(mockedList.get(1)).thenThrow(RuntimeException())4whenever(mockedList.get(2)).then { invocation ->5println(invocation.arguments[0])6"called with argument: ${invocation.arguments[0]}"7}8verify(mockedList).get(0)9verify(mockedList, times(2)).get(2)10verify(mockedList, atLeastOnce()).get(2)11verify(mockedList, atLeast(2)).get(2)12verify(mockedList, atMost(5)).get(2)13verifyNoMoreInteractions(mockedList)14doThrow(RuntimeException()).whenever(mockedList).clear()15val mockedList = mock<List<String>>()16whenever(mockedList.contains(argThat { it.length > 5 })).thenReturn(true)17val mockedList = mock<List<String>>()18whenever(mockedList.add(anyString())).thenAnswer { invocation ->19println(invocation.arguments[0])20}21mockedList.add("someString")22val mockedList = mock<List<String>>()23val argument = argumentCaptor<String>()24whenever(mockedList.add(argument.capture())).thenReturn(true)25mockedList.add("one")26mockedList.add("two")27val list = ArrayList<String>()28val spy = spy(list)29doReturn("foo").whenever(spy).get(0)30spy.add("one")31spy.add("two")32verify(spy).add("one")33verify(spy).add("two")34val mockedList = mock<List<String>>()35whenever(mock
Mockito
Using AI Code Generation
1val mock = mockk<MockClass>()2every { mock.method() } returns "mocked"3val mock = mock(MockClass::class.java)4`when`(mock.method()).thenReturn("mocked")5val mock = mockk<MockClass>()6every { mock.method() } returns "mocked"7val mock = mock(MockClass::class.java)8`when`(mock.method()).thenReturn("mocked")9val mock = mockk<MockClass>()10every { mock.method() } returns "mocked"11val mock = mock(MockClass::class.java)12`when`(mock.method()).thenReturn("mocked")13val mock = mockk<MockClass>()14every { mock.method() } returns "mocked"15val mock = mock(MockClass::class.java)16`when`(mock.method()).thenReturn("mocked")17val mock = mockk<MockClass>()18every { mock.method() } returns "mocked"19val mock = mock(MockClass::class.java)20`when`(mock.method()).thenReturn("mocked")21val mock = mockk<MockClass>()22every { mock.method() } returns "mocked"23val mock = mock(MockClass::class.java)24`when`(mock.method()).thenReturn("mocked")25val mock = mockk<MockClass>()26every { mock.method() } returns "mocked"27val mock = mock(MockClass::class.java)28`when`(mock.method()).thenReturn("mocked")29val mock = mockk<MockClass>()30every { mock.method() } returns "mocked"31val mock = mock(MockClass::class.java)32`when`(mock.method()).thenReturn("mock
Mockito
Using AI Code Generation
1val mock = mock<Calculator>()2val calc = Calculator(mock)3val result = calc.add(10,20)4verify(mock).add(10,20)5assertEquals(30,result)6}7}8}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!