Making Android Studio Test Coverage works with Kotlin and Robolectric

Android Studio has came a long way improving it’s capability in unit testing (thought still long way to go I think). Beginning as early as Android Studio 1.2 (if not mistaken), we could also see coverage testing. This could be as simple as a menu click away by right click on the Test Section of your code package, and then select Run ‘Test in…’ with Coverage as shown below.

Was fun having this this feature in place, seeing my coverage of my Java codes….. until one day I switch to Kotlin

Kotlin

Using Kotlin with the unit test coverage, still good. Kotlin has it’s various test challenges (I have a story on it). Coverage was still working good until we use nharman mockito for Kotlin library.

After debugging into the problem, the library is not at fault at all. It is a Kotlin language issue. By just adding as the keyword `inline` to a function as I reported in Stackoverflow, you’ll loose all the Coverage number.

Thankfully after checking out, we have two Options of Coverage feature in Android Studio, i.e. 1. IntelliJ IDEA and 2. JaCoCo. Apparently the problem only hit IntelliJ IDEA, but not JaCoCo.

Switching to JaCoCo by editing the Configuration, go to the Code Coverage Tab as below, makes our Coverage works again

We have quite a little while of happiness, and like usual, we didn’t run the Coverage by default, but only once in a blue moon…

--

--