我正在努力为我正在开发的库获得 100% 的代码覆盖率,但我似乎在 switch 语句和覆盖率方面遇到了一些问题,我根本不理解。
我目前正在使用 Jacoco 0.7.2,因为每个新版本似乎都与 Robolectrics 发生冲突。
我测试了一个简单的 switch 语句:
public enum Type {
NONE, LEGACY, AKS
}
private static Class<?> getCipherClass(Type type) {
switch (type) {
case LEGACY:
return CipherWrapperLegacy.class;
case AKS:
return CipherWrapperAks.class;
default:
return null;
}
}
我编写的测试包含以下检查(我必须使用反射,因为该方法是私有的):
final CipherWrapper instance = CipherWrapper.createInstance(mockContext, CipherWrapper.Type.LEGACY, ALIAS);
assertNotNull(instance);
Method getCipherMethod = TestUtils.makeMethodAccessible(CipherWrapper.class, "getCipherClass", CipherWrapper.Type.class);
assertNull(getCipherMethod.invoke(instance, CipherWrapper.Type.NONE));
assertEquals(CipherWrapperAks.class, getCipherMethod.invoke(instance, CipherWrapper.Type.AKS));
assertEquals(CipherWrapperLegacy.class, getCipherMethod.invoke(instance, CipherWrapper.Type.LEGACY));
结果不是我所期望的:
该图像有点令人困惑,因为黄线表明缺少某些东西。绿色图标告诉我 3 个分支中有 3 个被覆盖。
我还测试了延长开关盒case NONE
和一次失败,但这并没有改变任何事情。
我唯一能做的就是用 if/else 替换 switch,然后我就得到 100% 的覆盖率。
Currently I have 98% coverage but I nothing is missed based on the overview: