https://kotlinlang.org logo
Title
a

Anton Afanasev

02/15/2022, 4:48 PM
Hi all. I have a question regarding sequense/order verification. Consider following: Subject:
class Example (val dependency : Dependency, val anotherDependency: AnotherDependency ){
    fun doWork() {
        dependency.doSomething()
        anotherDependency.doSomethingElse()
    }
}


class Dependency() {
    fun doSomething() {
        println("do something")
    }
}

class AnotherDependency() {
    fun doSomethingElse() {
        println("do something")
    }
}
Test Class:
class ExampleTest {
    private val mockDependency = mockk<Dependency>(relaxed = true)
    private val mockAnotherDependency = mockk<AnotherDependency>(relaxed = true)

    val subject = Example(mockDependency, mockAnotherDependency)

    @Test
    fun `when doWork called`() {
        subject.doWork()

        verifyOrder {
            mockDependency.doSomething()
        }
    }
}
My tests are passing even though I never verified that
mockAnotherDependency.doSomethingElse()
invocation. I wonder if there any way to tell mockk to enforce that if any mock object participating in test-case was not verified the test case fails. Without that check I can seamlessly remove:
anotherDependency.doSomethingElse()
from my actual Example.doWork() and tests will still pass.
m

Mattia Tommasone

02/15/2022, 9:39 PM
If i understood your request correctly, there’s an open issue about it: https://github.com/mockk/mockk/issues/334
a

Anton Afanasev

02/16/2022, 3:13 PM
Thank you @Mattia Tommasone this is the behaviour I was looking for. I guess it can be very beneficial to verify mock interaction is strict, as it enforce that no mock invokation is missing. Is it something you have a priority for?
m

Mattia Tommasone

02/16/2022, 3:15 PM
not really, but i’d be glad to review a PR about it 🙂
1