mockito / mockito-kotlin

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

[Feature request] Support unsigned numbers #363

Open neworld opened 4 years ago

neworld commented 4 years ago

Info:

Test case:

    @Test
    fun mockitoTest() {
        val fixture: UnsignedFixture = mock()

        fixture.ubyte(42u)
        fixture.ushort(42u)
        fixture.uInt(42u)
        fixture.uLong(42u)

        verify(fixture).ubyte(eq(42u))
        verify(fixture).ushort(eq(42u))
        verify(fixture).uInt(eq(42u))
        verify(fixture).uLong(eq(42u))
    }

    interface UnsignedFixture {
        fun ubyte(value: UByte)
        fun ushort(value: UShort)
        fun uInt(value: UInt)
        fun uLong(value: ULong)
    }

Error:

Argument(s) are different! Wanted:
unsignedFixture.uInt-WZ4Q5Ns((UInt) 42);
-> at lt.libredrop.peerdiscovery.PeerDiscoverySpecsComplainTest.mockitoTest(PeerDiscoverySpecsComplainTest.kt:72)
Actual invocations have different arguments:
unsignedFixture.uInt-WZ4Q5Ns((Integer) 42);
-> at lt.libredrop.peerdiscovery.PeerDiscoverySpecsComplainTest.mockitoTest(PeerDiscoverySpecsComplainTest.kt:67)

Tried workarounds:

argThat

        verify(fixture).uInt(argThat {
            this == 42u
        })

throws:

java.lang.NullPointerException
    at lt.libredrop.peerdiscovery.PeerDiscoverySpecsComplainTest.mockitoTest(PeerDiscoverySpecsComplainTest.kt:117)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)

Capture

        val captor = argumentCaptor<UInt>()

        verify(fixture).uInt(captor.capture())

        assertEquals(42u, captor.firstValue)

Throws:

java.lang.NullPointerException
    at lt.libredrop.peerdiscovery.PeerDiscoverySpecsComplainTest.mockitoTest(PeerDiscoverySpecsComplainTest.kt:74)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
bohsen commented 4 years ago

Duplicate of #309

@neworld The issue mentions a workaround, but not sure it fits your needs. Also as long as Mockito-kotlin depends on mockito (java) internally I don't think there's an easy solution to this. Would need something like this to work.