Hi,
I have a Spring boot project that I am trying to generate code coverage for.
I found that the code coverage percentage reported goes up and down when running code coverage multiple times.
When I investigated further, noticed that a method in my Spring component that is annotated with @PreDestroy is sometimes partly covered, sometimes not at all (see attached image)
@PreDestroy
public void save() {
LOGGER.debug("Repository.save() called");
LOGGER.debug("Repository.save() called 1");
LOGGER.debug("Repository.save() called 2");
if (null == _items)
return;
LOGGER.debug("Repository.save() called 3");
try{
LOGGER.debug("Repository.save() called 4");
ObjectMapper mapper = new ObjectMapper();
LOGGER.debug("Repository.save() called 5");
mapper.writeValue(new File(_registrationFileName), _items);
LOGGER.debug("Repository.save() called 6");
} catch (Exception ex) {
LOGGER.debug("Repository.save() called 7");
LOGGER.error(ex.getMessage());
LOGGER.debug("Repository.save() called 8");
LOGGER.error(ex.getStackTrace().toString());
LOGGER.debug("Repository.save() called 9");
}
LOGGER.debug("Repository.save() called 10");
}
When I add log statements to check if the method is executed to the end, I can see the log statements in the console in STS(Eclipse) but the code is reported as not covered (or one or 2 lines are covered).
The console also reports that the PreDestroy method is being executed on Thread-4, not the main thread.
2017-09-25 09:36:07.962 INFO 19780 --- [ main] .o.R.RegistrationServiceApplicationTests : Started RegistrationServiceApplicationTests in 4.615 seconds (JVM running for 7.177)
2017-09-25 09:36:08.110 INFO 19780 --- [ Thread-4] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@4bf8b77: startup date [Mon Sep 25 09:36:04 BST 2017]; root of context hierarchy
2017-09-25 09:36:08.370 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called
2017-09-25 09:36:08.371 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 1
2017-09-25 09:36:08.371 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 2
2017-09-25 09:36:08.371 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 3
2017-09-25 09:36:08.371 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 4
2017-09-25 09:36:08.371 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 5
2017-09-25 09:36:08.426 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 6
2017-09-25 09:36:08.426 DEBUG 19780 --- [ Thread-4] c.n.o.R.repository.Repository : Repository.save() called 10
What settings am I missing to make JaCoCo include this method in it's code coverage calculations?
I am using JaCoCo plugin version 0.7.9, Maven 3.3.9, Spring 4.3.8.
I see the same inconsistent results when I run from the command line.
Thanks,
Ashley