3
在我们公司,我们的代码覆盖率要求为90%。Cobertura,Enums和Switch语句
运行的Cobertura报告,我只获得88.8%的覆盖率,和我看到下面的开关声明强调:
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);
}
为了确保,请添加您的测试代码。 – Markus
你的单元测试是否覆盖了所有四种可能的情况,包括'default'情况? – Jesper
单元测试使用TopBrandPrefix的每个可能的值调用此函数 – Xetius