I have been trying to generate code coverage for my web application using Cobertura and Junit but am running into problems. My webapp is a Java web application deployed on WebSphere Liberty Profile. I have followed the steps mentioned here: https://github.com/cobertura/cobertura/wiki/FAQ#using-cobertura-with-a-web-application
My steps are as follows:
- Instrument classes using cobertura-instrument ant task.
- Put cobertura.jar in the lib folder of my webapp (so that it is on the classpath)
- Start Liberty
- Run Junits (JUnit runs in a separate JVM other than the Liberty JVM)
The problem is that, cobertura.ser file is not generated when I stop liberty. I have tried the "hack" mentioned here: https://github.com/cobertura/cobertura/wiki/FAQ#using-cobertura-with-a-web-application
It seemed to work (I actually got some coverage info), but I was seeing that the cobertura.ser file was repeatedly being initialized to zero size and then the size increased to some number, so I am a little hesitant to use this. Moreover, this requires change in the code itself, and depends on the logout code being called, which is not quite ideal for automation.
But I am more interested in a setting for Websphere Liberty such as the one described for JBoss
-Djboss.shutdown.forceHalt=false
In particular, a jvm setting that would allow Cobertura to detect the JVM shutdown hook called by Liberty profile. Is there such a setting for WebSphere/Liberty?
The liberty profile doesn't ever call Runtime.halt so all shutdown hooks should be called appropriately. I thought I'd take a look to reproduce and I think I managed to get it working (I say I think because none of the command line scripts worked so I may still have done something wrong)
at the end I looked in the WEB-INF/classes and there was a cobertura.ser file that was 1480 bytes (i.e. non-zero.). When I ran the report tool on this it said I had no coverage, so I deleted the file and went back to reproduce. The cobertura.ser file in WEB-INF/classes wasn't generated. Instead I looked in the server working directory (in case it was there instead) and it wasn't. When I generated the report on this I got coverage.
So some possible things to look for: