Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Jacoco to use a specific version in Gradle

Based on the release notes in version 0.8.3 the not-null assertion operator is filtered out, I'm using Jacoco version 0.8.5 like this:

jacoco {
    toolVersion = "0.8.5"
}

But it's telling me that Not covered by tests (8 conditions)

Not covered by tests (8 conditions)

I'm using com.dicedmelon.gradle:jacoco-android Github link

I think toolVersion = "0.8.5" not working or something like that, so for that I need a way to force Jacoco version.

Is there any way to fix this issue?

like image 890
Mohamed Hamdan Avatar asked Jul 17 '20 17:07

Mohamed Hamdan


People also ask

Does JaCoCo support Kotlin?

JaCoCo is a free code coverage library that is used in most of the projects for measuring the test coverage for Java/Kotlin/Android.


1 Answers

Without seeing your code and your tests I can't say with 100% confidence but it looks like Jacoco is working fine and there are cases not covered there.

You're using the !! 3 times. When you use this operator, in reality, you're creating 2 flows, for when the variable is null and another for not-null. If you add tests for the cases where the variables are null, you should reach 100% coverage.

Just to make explicit, if you would handle your nullable with safe calls, you would have something like this:

val token = authResult.user?.let {
        authenticationDataSource.getIdToken(true, it)
            ?.let { it.token }
            ?: throw GetIdTokenResultIsNullException()
    } ?: throw UserIsNullException()

    token?.let {
        authenticationDataSource.loginBy(AuthenticationPayload(it))
    } ?: throw TokenIsNullException()

Wherever, I'm throwing exceptions you should handle that case as desired, and this is the alternate branch that is created by the nullability.

If you're sure that your values won't be null then you should change your types to make it clear and avoid extra checks.

On a side note, jacoco-android doesn't seem to be maintained anymore here and it's not compatible with newer gradle versions, so I would recommend using Jacoco directly. Migrating from jacoco-android to jacoco shouldn't be that hard.

like image 79
Douglas Kazumi Avatar answered Oct 19 '22 09:10

Douglas Kazumi