Not able to generate test coverage for Elvis operator. I was used jacoco and kover.
Asked
Active
Viewed 831 times
0
-
Are you testing all the various nulls? In other words, `dataSyncResponse`, `dataSyncResponse.userpreferences`, `dataSyncResponse.userpreferences.tutorialCompleted` :-) – karllindmark Mar 29 '22 at 06:51
-
@karllindmark Yes basically this is an interface function, I am using the Elvis operator for null safety. – Droid Mar 29 '22 at 07:05
1 Answers
0
If you put your mouse over the yellow highlight, Jacoco report would indicate how many branches are missed in code coverage. Your test is just hitting some cases.
Basically, jacoco sees your code like below.
// dataSyncResponse?.userpreferences?.tutorialCompleted?:""
if (dataSyncResponse != null) {
if (dataSyncResponse.userpreferences != null) {
return dataSyncResponse.userpreferences.tutorialCompleted
}
}
return ""
Did your test have provided a test case for
- null dataSyncResponse
- non null dataSyncResponse,
- null userpreferences
- non null userpreferences
- null tutorialCompleted
- non null tutorialCompleted

ocos
- 1,901
- 1
- 10
- 12
-
I know it's a little dated, but what if tutorialCompleted cannot be null? I have the same issue but I can't test that case and somehow jacoco doesn't give me 100% branch coverage – tagtraeumer Aug 24 '23 at 15:34
-
1@tagtraeumer I think Jacoco creates coverage report from Java bytecode not from source code written in java, kotlin ... If your goal is 100% branch coverage, you can either add a small java test code where `tutorialCompleted` is null or use java reflection to nullify the `tutorialCompleted`. – ocos Aug 27 '23 at 13:42
-