fun `to test argumentCaptor vs check `() {
val liveDataItem = MutableLiveData<Int>()
val observer = mock<Observer<Int>>()
liveDataItem.observeForever(observer)
liveDataItem.postValue(5)
// TEST1 : using argument captors works!!
argumentCaptor<Int>().apply {
verify(observer, times(1)).onChanged(capture())
allValues.size shouldBe 1
firstValue shouldBe 5
}
// TEST 2: with check does not work!!
// java.lang.AssertionError: expected: 1 but was: 2
var count = 0
verify(observer).onChanged(check {
count++;
it shouldBe 5
count shouldBe 1
})
}
only one value (5) is posted to liveDataItem, Observer is observing liveDataItem
I expected it to get the value only once. TEST 1 behaves as expected but TEST 2 is invoked twice.
Any explanation would be appreciated.
Versions
com.nhaarman:mockito-kotlin:1.6.0