mockito-kotlin
mockito-kotlin copied to clipboard
Mocking a function while using IR silently fails and returns null
If you have a project using Kotlin 1.4.21 (and mockito-kotlin 2.2.0) and you turn on the new IR backend, the mocking behavior changes.
Without IR, this mock seems to work private val validator: (String?, String?) -> Boolean = mock()
- but with IR enabled, validator
is null
.
With useIR = false
the following test passes:
private val function: (Int, Int) -> Int = mock()
@Test
fun addition_isCorrect() {
whenever(function.invoke(any<Int>(), any<Int>())).thenReturn(4)
val result = function.invoke(2, 2)
assertEquals(4, 2 + 2)
}
But switch it to useIR = true
and you get
java.lang.NullPointerException
at com.example.mocktiokotlinir.ExampleUnitTest.addition_isCorrect(ExampleUnitTest.kt:21)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Reproducer: https://github.com/bohsen/TestProject
Failing tests: https://github.com/bohsen/TestProject/blob/0203ad78a35feeb861b442e64e6c45dff97ab41b/src/test/kotlin/com/mockito/mockitokotlin2/MockingTest.kt#L355
Problem persists whenever you mock a lambda (or a Function
). Hopefully the new IR backend isn't about to be enabled by default just yet. Fails with both kotlin 1.4.21, 1.4.30-M1 and 1.4.30-RC.
Youtrack issue: https://youtrack.jetbrains.com/issue/KT-44429
Fixed in kotlin 1.5.