Reputation: 61
I have run into a jacoco code coverage issue where it complains about missing branches for the kotlin coroutines function withContext(Dispatchers.IO)
event though all branches are covered. Nothing fancy in my code it tries to load the authenticated user and returns non null Long value which is already covered.
Jacoco keeps complaining about it.
suspend fun resolveLoggedUserId(): Long = withContext(Dispatchers.IO) {
//...execute code base
}
This also happens for my generic function when I invoke it from actual methods:
suspend fun <T : Any> withLoggedUser(
context: Context,
block: suspend (Long) -> T,
): T = with(context) {
block()
}
example:
suspend fun doStuff(
request:Request,
): Response =
withLoggedUser(context = request.context) { loggedUser ->
Response.newBuilder()
.setId(loggedUserId)
.setAvailable(service.isAvailable(loggedUser))
.build()
}
It complains about missing branches at the withLoggedUser(context = request.context)
level (most likely null value) while the actual method returns non null.
Is this a knowning issue with jacoco not filtering such method.
You might also notice that I have also forced my generic upper bound T to Any and not Any?
and nothing changes.
Solution:
I was not passing any suspend function that 's why it is complaining, but otherwise it works now.(See my last comment) and this jacoco
Upvotes: 1
Views: 1056
Reputation: 10574
Without Complete Minimal Reproducible Example hard to say for sure, but this looks very similar to https://github.com/jacoco/jacoco/issues/1383 - for suspending lambdas without suspension points (i.e. without invocations of suspending functions/lambdas) JaCoCo 0.8.12 shows missed branch, this is planned to be fixed in future JaCoCo version - https://github.com/jacoco/jacoco/pull/1283
Upvotes: 0