Java 7 cobertura 代码分支覆盖字符串 switch 语句

标签 java unit-testing junit eclemma

我正在 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 版本中修复。

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

关于Java 7 cobertura 代码分支覆盖字符串 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22182618/

相关文章:

unit-testing - clojure.test/are 与 clojure.test/testing

java - 如何跟踪 junit 测试中的值?

Java:单元测试、JUnit、错误

java - 为什么 CodePro junit 测试方法会抛出异常(在 Eclipse 中)?

java - 如何断言该条件最终得到满足?

java - 参数为空时 Spring Cloud 假装行为

java - Struts2 验证 - 验证失败时重新填充子项

java - 添加 Jbutton 和 Jlabel

java - 拦截器和装饰器的区别

javascript - YUI.Test 测试在只有一个失败的断言后停止