jeudi 27 décembre 2018

Why Spring @Service methods appear with 0% code coverage in JaCoCo?

These libraries are loaded:

  • JUnit 5.3.2
  • JaCoCo 0.8.2
  • Mockito 2.10.0

Only element "static {...}" appears with 100% coverage. All the rest is at 0%:

JaCoCo report

The unit test class has annotations @ExtendWith(SpringExtension.class) and @AutoConfigureMockMvc. The service is injected with @Mock.

doReturn(actual).when(service).get(param);
when(service.get(param)).thenReturn(actual);

expected = service.get(param);
verify(service, times(1)).get(param);

assertEquals(expected, actual);
assertEquals(actual, expected);

My ServiceImpl class is red when I click any method. It extends an abstract class. Jackson's ObjectMapper is red, and also the entire lines within the methods. For example:

public CustomReturnObject get(final CustomParamObject paramObject) {
    try {
        return retryTemplate.execute(status -> {
            String json = repository.get(paramObject);
            CustomReturnObject returnObject = json2CustomObject(json, paramObject);

            if (returnObject == null) {
                returnObject = new CustomReturnObject();
                returnObject.setId(paramObject.getId());
            }

            return returnObject;
        });
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
        return null;
    }
}

Aucun commentaire:

Enregistrer un commentaire