I have a passing test below that uses an inline fun.
class SimpleClassTest {
private inline fun <reified T: Any> anyObject(): T {
return Mockito.anyObject<T>()
}
lateinit var simpleObject: SimpleClass
@Mock lateinit var injectedObject: InjectedClass
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testSimpleFunction() {
simpleObject = SimpleClass(injectedObject)
simpleObject.simpleFunction()
verify(injectedObject).settingDependentObject(anyObject())
}
}
But now I want to remove the inline fun as there's only one usage of it. So I replace the T with the actual class name i.e. Response (from Okhttp), and set it directly as a parameter in settingDependentObject
class SimpleClassTest {
lateinit var simpleObject: SimpleClass
@Mock lateinit var injectedObject: InjectedClass
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testSimpleFunction() {
simpleObject = SimpleClass(injectedObject)
simpleObject.simpleFunction()
verify(injectedObject).settingDependentObject(Mockito.anyObject<Response>())
}
}
It will fail with
java.lang.IllegalStateException: Mockito.anyObject<Response>() must not be null
How could I manually inline it correctly and behave the same as when I have the private inline fun <reified T: Any> anyObject(): T
?
Updated Note: this is a follow up question of Kotlin: Manually inlining a generic function produce a different result?. The previous provide the why, but I can't get to how to manually inline it properly.