Using Cobertura for Junit testing webapp deployed on WebSphere Liberty Profile

223 views Asked by At

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:

  1. Instrument classes using cobertura-instrument ant task.
  2. Put cobertura.jar in the lib folder of my webapp (so that it is on the classpath)
  3. Start Liberty
  4. 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?

1

There are 1 answers

1
Alasdair On

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)

  1. Wrote a simple servlet war
  2. Downloaded cobertura and put the cobertura-2.1.1.jar and all the jars in the downloads lib folder into the WEB-INF/lib of my war
  3. Ran the java InstrumentMain class to instrument the classes in WEB-INF/classes (choosing to overwrite them)
  4. Started the server
  5. Accessed the application
  6. Shutdown the server

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:

  1. Is this the corbertura.ser you are looking for? Look in the usr/servers/ folder to see if there is one there.
  2. Did the instrumented classes end up in the app prior to or instead of the non-instrumented ones
  3. Was the cobertura dependencies available.