mockito / mockito-kotlin

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

Behavior using argumentCaptor vs check for verifying observer of livedata #362

Closed rajab57 closed 5 years ago

rajab57 commented 5 years ago

    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
bohsen commented 5 years ago

@rajab57 Check #288 for an explanation.

Also you should probably consider updating mockito-kotlin to version 2.2.0 as 1.6.0 is quite outdated by now.

rajab57 commented 5 years ago

Thank you!