mockk icon indicating copy to clipboard operation
mockk copied to clipboard

Add lenient mocking for checkUnnecessaryStub

Open ebrowne72 opened this issue 1 year ago • 0 comments

Expected Behavior

Mockito has lenient stubbing, where a stub defined as lenient but not called during the test will not report an error when strict stubbing is enabled. I'd like to have the same functionality in MockK so that when checkUnnecessaryStub() or the CheckUnnecessaryStub annotation is used I can mark a stub as lenient and the test will still pass if the stub isn't called.

For example, in this case:

class SUT(val foo: Foo) {
    fun doSomething() {
        if (foo.bar && foo.baz) {
            .
            .
            .
        }
    }
}

every { foo.bar } returns false
every { foo.baz } returns true
sut.doSomething()
checkUnnecessaryStub()

This test will report that foo.baz is an unnecessary stub because the if statement will early-out and not call it. For readability and to make the test less brittle (if the if statement is re-ordered then the foo.baz stub would be needed) I would like to keep the stubbing for foo.baz but have checkUnnecessaryStub ignore that it wasn't called.

ebrowne72 avatar Jun 24 '24 22:06 ebrowne72