gpt4 book ai didi

java - Cobertura、枚举和 Switch 语句

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:53 24 4
gpt4 key购买 nike

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

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

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 是以下枚举类型:

public enum BrandParamType {

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

在我的单元测试中,我使用这些值中的每一个调用 getPrefix,那么为什么我在这里没有获得 100% 的分支覆盖率?

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

@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);
}

最佳答案

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

关于java - Cobertura、枚举和 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13819862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com