我正在 java 7 中编写一个 switch 语句,该语句根据字符串而变化。代码和测试本身相当简单,但由于某种原因,Cobertura(和 Eclemma)都表明我错过了交换机内的分支。
以下代码表明我错过了 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% 的分支覆盖率?
最佳答案
发现这是 Cobertura 中的一个已知错误,应该在 2.1.0 版本中修复。
关于Java 7 cobertura 代码分支覆盖字符串 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22182618/