Search code examples
tomcat6code-coveragereportcobertura

cobertura report without stopping tomcat


i followed the steps that are given for cobertura report and i generated report with following steps given in the URL Cobertura on Tomcat

but now my problem is to generate cobertura report without stopping tomcat


Solution

  • There are only 2 Ways to create a coverage data file.

    1. Stop Tomcat
    2. Execute a piece of code, that tells cobertura to write the file

    Regarding the Second approach: You have to call this function yourself, after your tests run. You could (for example) put this code in a servlet (which you call at the end of your tests).

    If you don't stop Tomcat or execute the function, you will not get a coverage data file.

    This is from the cobertura FAQ

    Cobertura only writes the coverage data file when the application server shuts down. We do not want to stop our application server after running our tests.
    It is possible to instruct Cobertura to write the data file. One of your classes should call the static method net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData(). For example, you could add something like this to a "logout" method in your web application:

    try {
        String className = "net.sourceforge.cobertura.coveragedata.ProjectData";
        String methodName = "saveGlobalProjectData";
        Class saveClass = Class.forName(className);
        java.lang.reflect.Method saveMethod = saveClass.getDeclaredMethod(methodName, new Class[0]);
        saveMethod.invoke(null,new Object[0]);
    } catch (Throwable t) {}