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

Vararg verification failures using mockito 4.11.0

Open ZOlbrys opened this issue 2 years ago • 0 comments

Consider the following code:

package com.xyz.packagexyz

open class TestService {
    fun doSomethingElse(message: String, vararg args: Any) {
        print("message: $message, args: $args")
    }
}

class TestClass(val service: TestService) {
    fun doSomething(message: String, vararg args: Any) {
        service.doSomethingElse(message, args)
    }
}

With code testing this:

internal class TestClassTest {
    private val mockService = mock<TestService>()
    private val testClass = TestClass(mockService)

    @Test
    fun test() {
        val message = "message"
        val arg1 = "arg1"
        testClass.doSomething(message, arg1)

        verify(mockService).doSomethingElse(message, arrayOf(arg1))
    }
}

Using mockito 4.11.0/mockito-kotlin 4.1.0, the test here fails with

Argument(s) are different! Wanted:
testService.doSomethingElse(
    "message",
    ["arg1"]
);
-> at com.xyz.packagexyz.TestService.doSomethingElse(TestClass.kt:5)
Actual invocations have different arguments:
testService.doSomethingElse(
    "message",
    ["arg1"]
);
-> at com.xyz.packagexyz.TestClass.doSomething(TestClass.kt:11)

Using mockito 4.10.0, the test above passes successfully. Any ideas?

This was also asked on stackoverflow which includes some workarounds that IMO do not make sense to use (I feel like a bug is happening with the code above), but maybe I am mistaken.

After discussion on https://github.com/mockito/mockito/issues/2856 it was requested to move the bug here. In that issue it was discovered that if the source code under test is written in Java and the verification is done using eq method such as:

verify(mockService).doSomethingElse(eq(message), eq(arg1))

the test passes (using mockito directly and/or with mockit-kotlin). It's only when using kotlin source code that this fails.

ZOlbrys avatar Jan 11 '23 21:01 ZOlbrys