首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cobertura、Enums和Switch语句

Cobertura、Enums和Switch语句
EN

Stack Overflow用户
提问于 2012-12-11 19:55:13
回答 1查看 1.4K关注 0票数 3

在我们公司,我们有90%的代码覆盖率要求。

运行Cobertura报告,我只获得了88.8%的覆盖率,并且我看到以下switch语句突出显示:

代码语言:javascript
复制
public TopBrandPrefix getPrefix() {
     switch(brandParamType) {
         case TOP12SUCHAS_AND:
             return TopBrandPrefix.SUCHAS;
         case TOP12COMME_ET:
             return TopBrandPrefix.COMME;
         case TOP12WIE_UND:
             return TopBrandPrefix.WIE;
         default:
             return TopBrandPrefix.NULL;
     }
 }

它报告了80%的覆盖率。brandParamType具有以下枚举类型:

代码语言:javascript
复制
public enum BrandParamType {

    TOP123,
    TOP456,
    TOP123LINKED,
    TOP456LINKED,
    TOP12,
    TOP12AND,
    TOP12SUCHAS_AND,
    TOP12COMME_ET,
    TOP12WIE_UND
}

在我的单元测试中,我用这些值调用了getPrefix,那么为什么我在这里没有得到100%的分支覆盖率呢?

我正在运行的单元测试如下:

代码语言:javascript
复制
@Test
public void testGetPrefixWithTOP123() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP123);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.NULL, prefix);
}

@Test
public void testGetPrefixWithTOP456() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP456);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.NULL, prefix);
}

@Test
public void testGetPrefixWithTOP123LINKED() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP123LINKED);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.NULL, prefix);
}

@Test
public void testGetPrefixWithTOP456LINKED() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP456LINKED);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.NULL, prefix);
}

@Test
public void testGetPrefixWithTOP12() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP12);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.NULL, prefix);
}

@Test
public void testGetPrefixWithTOP12AND() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP12AND);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.NULL, prefix);
}

@Test
public void testGetPrefixWithTOP12SUCH_AS() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP12SUCHAS_AND);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.SUCHAS, prefix);
}

@Test
public void testGetPrefixWithTOP12COMME_ET() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP12COMME_ET);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.COMME, prefix);
}

@Test
public void testGetPrefixWithTOP12WIE_UND() {
    TopBrandTouchpointParameterSource source = new TopBrandTouchpointParameterSource(urlComposer, TouchpointParameterHelper.BrandParamType.TOP12WIE_UND);
    TopBrandTouchpointParameterSource.TopBrandPrefix prefix = source.getPrefix();
    assertEquals(TopBrandTouchpointParameterSource.TopBrandPrefix.WIE, prefix);
}
EN

回答 1

Stack Overflow用户

发布于 2013-01-19 21:37:59

运行一个非常类似的测试,我看到100%的覆盖率。但是,当我在代码更改后运行mvn cobertura:cobertura,但没有运行clean时,我得到的报告只覆盖了部分分支。尝试删除您的Cobertura状态。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13819862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档