Sorry if the question has already been posted, but I didn't found something similar.
I'm using Jacoco since few days and I've found some branches seems not possible to test => and Jacoco tells me that code is not 100% tested.
Let's imagine I've the following Enum :
MyEnum {
CASE1,
CASE2,
CASE3;
}
And a method to test :
public String doIt(MyEnum myEnum) throws Exception {
switch (myEnum) {
case CASE1:
return "1";
break;
case CASE2:
return "2";
break;
case CASE3
return "3";
break;
default:
// Here it's only to avoid warning in eclipse :
// missing defaut case in switch...
throw new Exception("Unexpected Enum value: " + myEnum) ;
}
}
I've written all unit tests possible (with all MyEnum values) but Jacoco said that my test is 75% tested. The reason is that default is never tested.
But I've no idea how I can test it.
Calling doIt(null) will lead in a NullPointerException and does not resolve my problem.
I'm a little bit disapointed because I have to add default to avoid warnings and now I've have bad scoring test reports.
Is there a solution to enhance this ?
I expect the solution is not to relace switch by if...
Thanks