2

What is the difference between using verify(exactly = 0) and using wasNot called assertions of MockK when testing Kotlin?

I have an example where the former passes the test but the latter yields:

java.lang.AssertionError: Verification failed: call 1 of 1: KLogger(#1).error(any())) was not called.

Code sample:

private val logger = mockk<KLogger>()
...
@Test
fun `logMessage should log message with info log level`() {
...
every { logger.info(logMessage) } just runs
...
verify(exactly = 1) { logger.info(logMessage) }
verify { logger.error(any<String>()) wasNot Called }
}
Balu
  • 522
  • 6
  • 16

1 Answers1

1

For checking if a function on the mock was called or not, verify must be used based on this comment:

Construction like verify { mock wasNot Called } is used not for function but for whole mock

This works correctly:

verify(exactly = 0) { logger.error(any<String>()) }
Balu
  • 522
  • 6
  • 16