mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 201 forks source link

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

Open larmic opened 5 years ago

larmic commented 5 years ago
@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.

bohsen commented 5 years ago

// 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().

larmic commented 5 years ago

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.

nhaarman commented 5 years ago

Would Mockito's Strictness API help here?