Skip to content

Commit

Permalink
Adapted mockk in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamKwokX committed Sep 19, 2023
1 parent b4bb787 commit 8010f2d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
5 changes: 1 addition & 4 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ dependencies {
implementation libs.lifecycle.viewmodel.ktx
implementation libs.coreKtx

// androidTestImplementation libs.mockito.core
// androidTestImplementation libs.bytebuddy.android
// androidTestImplementation libs.mockito.inline
androidTestImplementation libs.mockito.android
androidTestImplementation libs.mockk
androidTestImplementation libs.mockk.agent
androidTestImplementation libs.kotlin.test
androidTestImplementation libs.junit
androidTestImplementation libs.androidx.fragment.test
Expand Down
31 changes: 20 additions & 11 deletions api/src/androidTest/java/pers/shawxingkwok/mvb/Observe.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package pers.shawxingkwok.mvb
package pers.shawxingkwok.mvb

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.testing.launchFragment
import androidx.lifecycle.Lifecycle
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.mockkObject
import io.mockk.verifyOrder
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import org.junit.Test
Expand All @@ -19,17 +21,20 @@ internal class Observe {
}

internal class MyFragment : Fragment() {
private val names = mutableListOf<String>()
companion object{
fun act(lifecycle: Lifecycle, name: String){
assert(lifecycle.currentState == Lifecycle.State.STARTED)
}
}

private fun act(name: String){
names += name
assert(lifecycle.currentState == Lifecycle.State.STARTED)
init {
mockkObject(MyFragment)
}

private val x: Flow<Int> by rmb { flowOf(1) }.observe { act("x") }
private val y by saveMutableStateFlow { 1 }.observe { act("y") }
private val z by saveMutableSharedFlow<_, Int>(replay = 1).observe { act("z") }
private val a by saveMutableLiveData { 1 }.observe { act("a") }
private val x: Flow<Int> by rmb { flowOf(1) }.observe { act(lifecycle, "x") }
private val y by saveMutableStateFlow { 1 }.observe { act(lifecycle, "y") }
private val z by saveMutableSharedFlow<_, Int>(replay = 1).observe { act(lifecycle, "z") }
private val a by saveMutableLiveData { 1 }.observe { act(lifecycle, "a") }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -38,8 +43,12 @@ internal class Observe {

override fun onResume() {
super.onResume()
assertAll(::x, ::y, ::z, ::a){
assert(it.name in names)
// verifyAll does not work in this case.
verifyOrder {
act(any(), "x")
act(any(), "y")
act(any(), "z")
act(any(), "a")
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ agp = "7.4.2"
kotlin = "1.8.10"
compileSdk = "33"
minSdk = "21"
mockkAgent = "1.13.7"
targetSdk = "33"
lifecycle = "2.6.1"
mockito = "5.5.0"
Expand Down Expand Up @@ -32,9 +33,10 @@ mockito-android = { module = "org.mockito:mockito-android", version.ref = "mocki
mockito-inline = { module = "org.mockito:mockito-inline", version = "5.2.0" }
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version = "4.1.0" }
mockito-dexmaker = { module = "com.linkedin.dexmaker:dexmaker-mockito", version = "2.12.1" }
mockk = { group = "io.mockk", name = "mockk", version = "1.12.8" }
mockk = { group = "io.mockk", name = "mockk-android", version = "1.13.7" }
bytebuddy-android = { module = 'net.bytebuddy:byte-buddy-android', version = '1.14.8' }

mockk-agent = { module = "io.mockk:mockk-agent", version.ref = "mockkAgent" }
viewbindingKtx = { module = "com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-nonreflection-ktx", version = "2.1.0" }

shawxing-ktUtil = { group = "io.github.shawxingkwok", name = "kt-util", version = "1.0.2" }
Expand Down

0 comments on commit 8010f2d

Please sign in to comment.