Skip to content

NPE when using result of suspending method of verify on Kotlin 2.0 #527

Open
@dzmpr

Description

@dzmpr

When there is a suspending method that return some built-in type (Boolean, Int) NPE "Cannot invoke "java.lang.Boolean.booleanValue()"" was thrown when storing verify method call result in variable, on calling verify inside if or when. There is no exception when verify used as last expression of Unit-returning method, or when mocked method is not suspending.
Here code to reproduce this bug:

interface TestContract {

    suspend fun getSuspend(): Boolean

    fun getSynchronous(): Boolean
}

class MockitoReproducer {

    private val contractMock = mock<TestContract>()

    @Test
    fun `should not fail with synchronous method`() = runTest {
        // Save result to variable. result is false
        val result = verify(contractMock, never()).getSynchronous()
        // Not save result to variable
        verify(contractMock, never()).getSynchronous()
    }

    @Test
    fun `should not fail with suspending method when not save result to variable`() = runTest {
        verify(contractMock, never()).getSuspend()
    }

    @Test
    fun `should fail with suspending method when save result to variable`() = runTest {
        val result = verify(contractMock, never()).getSuspend()
    }

    @Test
    fun `should fail with suspending method when use if as expression`() = runTest {
        if (true) {
            verify(contractMock, never()).getSuspend()
        } else {
            verify(contractMock, never()).getSuspend()
        }
        Unit // To explicitly specify that I not use if as result of lambda
    }

    @Test
    fun `should fail with suspending method when use when as expression`() = runTest {
        when {
            true -> verify(contractMock, never()).getSuspend()
            else -> verify(contractMock, never()).getSuspend()
        }
        Unit // To explicitly specify that I not use if as result of lambda
    }
}

Last three tests not passing.

I also created issue in kotlin youtrack in case it is language, not library related bug: link.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions