【问题标题】:Java 7 cobertura code branch coverage with string switch statements带有字符串 switch 语句的 Java 7 cobertura 代码分支覆盖
【发布时间】: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% 的分支覆盖率?

【问题讨论】:

标签: java unit-testing junit eclemma


【解决方案1】:

发现这是 Cobertura 中的一个已知错误,应该在 2.1.0 版本中修复。

https://github.com/cobertura/cobertura/issues/79

【讨论】:

  • 您应该通过接受您的回答来“回答您自己的问题” - 嘿,您会因此获得声誉积分!
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多