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

Mockito cannot mock/spy because : VM does not support modification of given type #441

Open
MastaP opened this issue Sep 9, 2021 · 2 comments

Comments

@MastaP
Copy link

MastaP commented Sep 9, 2021

Hi,

After switching kotlin's api/language from 1.4 to 1.5, the following code:

    @Test
    fun test() {
        val deferredSupplier = spy(Supplier { Flux.empty<String>() })
        Flux.defer(deferredSupplier).blockLast()
        verify(deferredSupplier, times(1)).get()
    }

fails with

Cannot mock/spy class com.example.Test$$Lambda$318/0x00000008001dc840
Mockito cannot mock/spy because :
 - VM does not support modification of given type

I'm using org.mockito.kotlin:mockito-kotlin:3.2.0 and org.mockito:mockito-inline:3.12.4
The issue is reproduced in the following project, where the only difference between two commits is changing Kotlin's api from 1.4 to 1.5
https://github.com/MastaP/mockito-kotlin15-bug/actions
Kotlin version: 1.5.30

@abelkov
Copy link

abelkov commented Sep 15, 2021

The reason for the behavior change is that since Kotlin 1.5, the Kotlin compiler uses the JVM invokedynamic instruction for SAM adapters (lambdas which are passed where Java interface is expected) by default, as javac does. See https://youtrack.jetbrains.com/issue/KT-47307#focus=Comments-27-4994817.0-0.

As a workaround, you could change the code to use an anonymous object instead of a SAM lambda:

val deferredSupplier = spy(object : java.util.function.Supplier<Any> {
    override fun get(): Any = Flux.empty<String>()
})

Or you can compile the test source set with this compiler argument:

compileTestKotlin {
    kotlinOptions.freeCompilerArgs += ['-Xsam-conversions=class']
}

cc @udalov can something be improved on the Kotlin side or this is purely a Mockito issue?

@MastaP
Copy link
Author

MastaP commented Sep 16, 2021

@abelkov thanks, the workaround helped

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

2 participants