Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dzmpr opened this issue Sep 18, 2024 · 0 comments
Open

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

dzmpr opened this issue Sep 18, 2024 · 0 comments

Comments

@dzmpr
Copy link

dzmpr commented Sep 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant