mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.1k stars 200 forks source link

Order matters for doReturnConsecutively setup (skips first item) under specific conditions #397

Open jlapierre opened 3 years ago

jlapierre commented 3 years ago

doReturnConsecutively skips the first item provided in the list, but only if the argument is 0 and the mocked call to method(0) is the first of mutiple calls to method() set up in the mock.

Example:

               mock {
                    on { getObject(eq(0)) } doReturnConsecutively listOf(111000333L, 222000333L)
                    on { getObject(eq(1)) } doReturnConsecutively listOf("Jeremy", "Bearimy")
                }

getObject(0) returns: 222000333L, 222000333L <- first item is skipped (last item repeats) getObject(1) returns: "Jeremy", "Bearimy"

But if I switch the order:

              mock {
                    on { getObject(eq(1)) } doReturnConsecutively listOf("Jeremy", "Bearimy")
                    on { getObject(eq(0)) } doReturnConsecutively listOf(111000333L, 222000333L)
                }

getObject(0) returns: 111000333L, 222000333L getObject(1) returns: "Jeremy", "Bearimy"

Changing the return type for getObject(0) does not matter (e.g. ints or strings) If I add more items to the list, it returns all but the first.

Finally, changing the argument from 0 to 2 in the mock and calling getObject(2) returns the correct values regardless of setup order.

In summary, the first item in the result of doReturnConsecutively is skipped only if all of the following conditions occur: