https://kotlinlang.org logo
#code-coverage
Title
# code-coverage
j

Joe

06/21/2019, 5:49 PM
Having a coverage question with respect to using the
?:
elvis operator to provide a default: If I have code that does something like:
Copy code
val x = nullableObject?.method() ?: default
JaCoCo tells me I have one of 4 branches uncovered. If I change that to:
Copy code
val nullableX = nullableObject?.method()
    val x = nullableX ?: default
JaCoCo tells me all branches are covered (2 branches per line). I'm guessing the "uncovered" branch is when
nullableObject
is non-null, but
method()
returns null -- is there a way to get JaCoCo to realize that branch is not possible based on
method()
not being nullable? Or is there a better practice here?
a

Alexis Cala Martínez

01/20/2021, 3:37 AM
Hello how did you solve this, i got a lot of cases like this and is a pin in the ...
j

Joe

01/20/2021, 3:48 PM
no great solutions here, there's the 2 line version if need full coverage, or just deal with the uncovered branch depending on the context
a

Alexis Cala Martínez

01/21/2021, 3:11 PM
ok thanks
45 Views