mockito / mockito-kotlin

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

Kotlin 2.0 / K2 compiler: lenient().whenever() causes NullPointerException #517

Closed tjalling-ran closed 1 month ago

tjalling-ran commented 1 month ago

Using Kotlin 2.0, the following line of code causes a NullPointerException at runtime:

lenient().whenever(countryRepository.findByIsoCodeAlpha2(any())).thenReturn(CountryMother.countryNL)

This is thrown from an intrinsics check generated by the compiler. See the decompiled Java:

image

In an older version of the compiler (screenshot is based on 1.9.21), this check is not generated by the compilor at all, which I assume is why there is no issue in earlier versions of kotlin.

See the decompiled java below:

image

Not sure whether this belongs here or needs to be solved upstream somehow. If so, maybe there's a workaround of some sort in the meantime?

tjalling-ran commented 1 month ago

Never mind, I forgot to try the latest version of mockito-kotlin first. Looks like it works with 5.3.1. (We were on 4.1.0)