我正在 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% 分支覆盖率?