Skip to content

verifyAll requires a check on all methods on a mock #74

@hoombar

Description

@hoombar

It looks like the verifyAll function requires a verification for all methods on a mock that have an interaction when a function is called.

Take the below example:

class Test(val dummy: Dummy) {
    fun doStuff() {
        dummy.method1(listOf("value"))
        dummy.method2()
    }
}
interface Dummy {
    fun method1(value: List<String>)
    fun method2(): String
}

@Test
fun verifyAllCheck() {
    val mock = mockk<Dummy>(relaxed = true)
    every { mock.method2() } returns "method2"

    val test = Test(mock)
    test.doStuff()

    // works
    verify { mock.method1(match { it.size == 1 }) }
    // does not work
    verifyAll {
        mock.method1(match { it.size == 1 })
    }
}

When you run the test, you get this message

java.lang.AssertionError: Verification failed: some calls were not matched: [Dummy(dummy).method2()]

You can make the verifyAll section pass by adding mock.method2() in to the verification, because the doStuff() method would invoke both method calls. It is suggested that this is as per design, but if I have a couple of interactions with my mock, I might not necessarily be interested in those interactions, but would currently be forced to add them in to the verifyAll block.

What I was trying to achieve was to be able to run multiple verifications on the list that was passed in to the method:

    verifyAll {
        mock.method1(match { it.size == 1 })
        mock.method1(match { it.get(0) == "value })
    }

Perhaps there's an alternative way of doing this, but the intuitive method seemed to be verifyAll. Maybe the introduction of matchAll {} would be a good route forwards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions