4

In our company, we have a Code Coverage requirement of 90%.

Running a Cobertura report, I am only getting 88.8% coverage, and I am seeing the following switch statement highlighted:

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

It is reporting 80% coverage. brandParamType is of the following Enum type:

public enum BrandParamType {

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

In my Unit Tests, I am calling getPrefix with each of these values, so how come I am not getting 100% branch coverage here?

The Unit Tests I am running are as follows:

@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);
}
Xetius
  • 44,755
  • 24
  • 88
  • 123

1 Answers1

1

Running a very similar test I see 100% coverage. However, I got a report of only partial branch coverage when I ran mvn cobertura:cobertura after code changes but without clean. Try deleting your Cobertura state.

Joe
  • 29,416
  • 12
  • 68
  • 88