How having 100% coverage in switch based on enum

3,616 views
Skip to first unread message

sylvain...@gmail.com

unread,
Mar 21, 2016, 1:19:14 PM3/21/16
to JaCoCo and EclEmma Users
Hi every body,

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

Marc R. Hoffmann

unread,
Mar 21, 2016, 1:33:08 PM3/21/16
to jac...@googlegroups.com
Hi,

JaCoCo does not understand the semantics of your code. It simply reports
what has been actually executed.

Future versions of JaCoCo might support filtering for such constructs. I
just added your use case:

https://github.com/jacoco/jacoco/wiki/FilteringOptions

Regards,
-marc
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages