mockito-kotlin icon indicating copy to clipboard operation
mockito-kotlin copied to clipboard

Improvement: Don't check whenever(mock)-calls on verifyNoMoreInteractions(mock)

Open larmic opened this issue 6 years ago • 3 comments

@Test
internal fun `some testing`() {
     val pong = Pong()
     whenever(mock.ping()).thenReturn(pong)
     assertThat(pingService.sendPing()).isEqualTo(pong)

     // this verification is already defined by whenever(...) 
     verify(mock).ping()
     // but is is needed, because otherwise this will fail
     verifyNoMoreInteractions(mock)
}

It will be reduce code if verifyNoMoreInteractions will not check whenever-calls or this could be configurable by an optional parameter.

larmic avatar Oct 02 '18 19:10 larmic

// this verification is already defined by whenever(...) verify(mock).ping()

verify(...) checks that mock.ping() is invoked. Whenever(...) verifies nothing, but just defines what should happen if mock.ping() is invoked. And this depends on the implementation of pingService.sendPing().

bohsen avatar Oct 02 '18 21:10 bohsen

Yes you are right. But a verifyNoMoreInteractionsAndVerifyWhenever could help.

My tests often look like the one described above. And (for me) it feels like duplicating code.

larmic avatar Oct 03 '18 13:10 larmic

Would Mockito's Strictness API help here?

nhaarman avatar Dec 31 '18 10:12 nhaarman