【发布时间】:2014-03-04 20:43:48
【问题描述】:
我正在 java 7 中编写一个 switch 语句,它根据字符串而变化。代码和测试本身相当琐碎,但出于某种原因,Cobertura(和 Eclemma)都表明我错过了 switch 中的分支。
以下代码表明我错过了 10 个分支中的 3 个:
public String decodeQuestionResponseType(final String questionResponseType){
switch (questionResponseType) {
case "multipleChoiceResponse":
return "multipleChoice";
case "textResponse":
return "text";
case "photoResponse":
return "photo";
default:
return "none";
}
}
@Test
public void testDecoder(){
assertEquals("multipleChoice", decodeQuestionResponseType("multipleChoiceResponse"));
assertEquals("text", decodeQuestionResponseType("textResponse"));
assertEquals("photo", decodeQuestionResponseType("photoResponse"));
assertEquals("none", decodeQuestionResponseType("otherResponse"));
}
我可以使用 if/else 语句进行编写,并且测试将通过。有什么我想念的吗?为什么我无法获得此代码 100% 的分支覆盖率?
【问题讨论】:
-
关于 EclEmma 你可以看看这个答案:stackoverflow.com/a/28015212/584532
标签: java unit-testing junit eclemma