Kotlin inline keyword causing IntelliJ IDEA Covera

2019-04-19 19:25发布

问题:

I created a very simple test function as below

class SimpleClassTest {

    lateinit var simpleObject: SimpleClass
    @Mock lateinit var injectedObject: InjectedClass


    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
    }

    @Test
    fun testSimpleFunction() {
        simpleObject = lookupInstance()
    }

    inline fun lookupInstance() = SimpleClass(injectedObject)
}

I Run it with Coverage... The test coverage number is 0%. But if I remove the inline keyword, the test coverage number shows now.

Is this a Kotlin issue or Android IntelliJ IDEA Coverage issue? (note: JaCoco coverage is good).

Note: I'm using Android Studio 2.0 and Kotlin 1.0.2

回答1:

When an inlined function is compiled, the compiler essentially pastes its body into the call site (in place of the function call). This means that the coverage analysis can't tell that it's an inlined function because it doesn't really exist where you defined it. In other words, this behavior is a natural artifact of what it means for a function to be inlined.