I'm writing some unit tests for my Android app written in Kotlin, and I'm getting errors from the included inlined collection functions, in this case specifically sortedBy
override fun onDaysSelected(dayOfWeekList: ArrayList<DayOfWeek>) {
view.userRoutingRule.days = dayOfWeekList.sortedBy { it.dayOfWeek }
renderRule()
}
This is the error I'm getting when I run my unit tests with coverage
---- IntelliJ IDEA coverage runner ---- sampling ... include patterns: com.mypackage..* exclude patterns:[2019.02.02 14:49:40] (Coverage): Class data was not extracted: com.mypackage\myfile$onDaysSelected$$inlined$sortedBy$1: java.lang.Throwable
Process finished with exit code 0
My unit tests all pass accordingly, but when I go to look at the coverage report, it's almost completely bare as this error stopped it from completing.
Is there any solution to this at this point? It's hard to know if I missed some condition if I can't just look at the report.
Line coverage reports on the execution footprint of testing in terms of which lines of code were executed to complete the test. Edge coverage reports which branches or code decision points were executed to complete the test. They both report a coverage metric, measured as a percentage.
The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.
Aim for 95% or higher coverage with unit tests for new application code. When developers unit test as they program, they improve the longevity and quality of the codebase. The time a development team invests in unit tests pays off with less time spent troubleshooting defects and analyzing problems later.
It's a known issue with calculating coverage of imline methods that use lambdas. (see No coverage report for inlined Kotlin methods and https://discuss.kotlinlang.org/t/inline-functions-coverage/5366)
If you want to run coverage in the current state, you'll need to use other approaches, for example implementing Comparable
interface in your DayOfWeek
and using .sorted()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With