0

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.

Community
  • 1
  • 1
Elye
  • 53,639
  • 54
  • 212
  • 474
  • 1
    Possible duplicate of [Kotlin: Manually inlining a generic function produce a different result?](http://stackoverflow.com/questions/37375142/kotlin-manually-inlining-a-generic-function-produce-a-different-result) – Ilya May 23 '16 at 23:35
  • @llya, it's not a duplicate. The http://stackoverflow.com/questions/37375142/kotlin-manually-inlining-a-generic-function-produce-a-different-result provides the why, but I can't get to how to manually inline it properly. So this is a follow up separate question. – Elye May 23 '16 at 23:49
  • You end up with the same code and it doesn't work for the same reason. – Ilya May 24 '16 at 00:02
  • Is there's no way of writing it without the inline function? – Elye May 24 '16 at 00:04
  • @Elye The tests with `fun anyObject()` in the question is failing with the very same reason as your later example. – miensol May 24 '16 at 05:31
  • @Elye You don't have to use an inline function it can be a regular function i.e. `fun anyObject(): T { return Mockito.anyObject() }`. [This ticket](https://youtrack.jetbrains.com/issue/KT-8135) desribes the questionable behavior. – miensol May 24 '16 at 05:42
  • Sorry, I guess I didn't put my question clear. I want to remove not just the inline, but the entire anyObject() function I made... so I could use Mockito.anyObject directly for my `settingDependentObject` function. Is that possible I could use Mockitoy.anyObject() directly, without having the `must not be null` error message? – Elye May 24 '16 at 06:15
  • 1
    @Elye As mentioned in [the linked answer](http://stackoverflow.com/a/37377126/155213) it's not possible to _inline it manually_ due to issue described in [this ticket](https://youtrack.jetbrains.com/issue/KT-8135). You can ease your pain with [the library](https://github.com/nhaarman/mockito-kotlin). – miensol May 24 '16 at 06:35
  • Thanks Miensol. That explains all!. I can't use that library fully due to two issues reported in http://stackoverflow.com/questions/37380830/java-lang-reflect-invocationtargetexception-when-using-kotlin-mokito-library and http://stackoverflow.com/questions/37403085/after-adding-mockito-kotlin-library-i-loose-my-coverage-data-of-android-studio. I do have workaround though, but just thought of getting my mind clear on what's happening with all these. Thanks!! – Elye May 24 '16 at 07:08

0 Answers0