mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.09k stars 198 forks source link

Kotlin spy beans created with incorrect method signature #505

Open petergphillips opened 7 months ago

petergphillips commented 7 months ago

Originally raised as Upgrade to Spring Framework 6.1 causes null pointer in CoroutinesUtils, but investigated by Spring there and considered to be a bug here instead.

I've got a reactive kotlin spring boot repository that I'm attempting to spy. The superclass (CoroutineCrudRepository) has methods such as:

    suspend fun findById(id: ID): T?

When autowiring the repository interface using Spring this gets created with a proxy signature of

public final java.lang.Object jdk.proxy3.$Proxy98.findById(java.lang.Object,kotlin.coroutines.Continuation)

Creating a spy from the repository using

mock(defaultAnswer = AdditionalAnswers.delegatesTo(exampleRepository))

then seems to end up with two versions of the findById method:

public java.lang.Object uk.co.greenthistle.coroutinespybeanexample.ExampleRepository$MockitoMock$poIc1iZ2.findById(java.lang.Object,kotlin.coroutines.Continuation)

and also

public java.lang.Object uk.co.greenthistle.coroutinespybeanexample.ExampleRepository$MockitoMock$poIc1iZ2.findById(java.lang.Long,kotlin.coroutines.Continuation)

This seems to then cause the new spring boot code to choose the wrong method and then not be able to find the correct method to call on the spy.

Please see example project demonstrating the issue at https://github.com/petergphillips/spring-coroutine-spybean-example