Detect unnecessary stubbings
See original GitHub issueMockito throws an exception when a function is stubbed and then not actually invoked.
This is particularly useful when doing TDD, or generally when writing tests first, because it allows to stub a function, have the test fail because it was not invoked, and then make it pass by implementing the invocation.
We could add a parameter to the mockk()
function and the @MockK
annotation, or a configuration flag to make it global, so that it works somewhat like this:
class Test {
@MockK(detectUnnecessaryStubbing = true)
private lateinit var collaborator: Collaborator
private lateinit var service: Service
@BeforeEach
fun setup() {
MockKAnnotations.init(this) // Perhaps this could be another good place to enable the "unnecessary stubbing detection mode"
service = Service(collaborator)
}
@Test
fun test() {
every { collaborator.function() } returns "foo"
service.anotherFunction() // Not executing collaborator.function in here will fail
}
}
A potential issue I see in this is the case when there are two tests executing service.anotherFunction()
, one stubbing and invoking collaborator.function()
and one not stubbing nor invoking it.
We need to find a way to detect that it’s ok for the second test not to invoke collaborator.function()
even if it was stubbed in another test.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8
Top GitHub Comments
Implemented in 1.12.4.
Great, I’m willing to test a version of MockK with this new behaviour as soon as is available!