2

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

Community
  • 1
  • 1
vinod
  • 1,115
  • 4
  • 13
  • 24

1 Answers1

0

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) {}
oers
  • 18,436
  • 13
  • 66
  • 75
  • thanks for the reply. in my source code i have large no of java files..so i cant add above one to source code.my second question with out adding to source code can we generate report. – vinod Dec 23 '11 at 08:52
  • you can't create a report, without writing the coverage data prior. You need to stop Tomcat OR execute the java code. There is no way around it. – oers Dec 23 '11 at 09:07
  • thank u for the reply.. i have lot of java programs that is my problem – vinod Dec 23 '11 at 10:12
  • hi oers i have written the above code in the java program but still i am unable to get coverage report. i am having one doubt i have written in regular java program not in logout method.when i am running my test cases i am not the touching the java class file where i have written above code then still it gives report. – vinod Dec 23 '11 at 10:22
  • You have to execute the code at some point, it won't run automatically. Maybe put it in a special Servlet or something that fits your needs. Maybe you are looking in the wrong place for the file, [try specifying](http://cobertura.sourceforge.net/faq.html) **-Dnet.sourceforge.cobertura.datafile=DIRECTORY/cobertura.ser** for tomcat. This will define where cobertra.ser will appear. – oers Dec 23 '11 at 10:41
  • i think this is the not correct one to generate report with out stopping tomcat. in my test case i have not covered the java class in which it has the above code then it doesn't create report. if they source code is too large then we cant include the code in the program – vinod Dec 23 '11 at 10:52
  • If you can't execute the code you have to stop Tomcat. There is no third way. – oers Dec 23 '11 at 11:07
  • what is executing the code means??? i am sorry if am asking silly questions and troubling u. i am thank fully for the replies u gave it helped me a lot – vinod Dec 23 '11 at 11:11
  • You have to write a java class with a method that contains the code above. You have to include this code in somewhere your web application (maybe a special servlet). After all your tests are done you have to call/execute/run this method on your tomcat. I can't put it any clearer, sry. – oers Dec 23 '11 at 11:46
  • when we write any another program or servlet which contains above source code in separate module or other source code rather than project then its show its coverage report rather than project report(may be i am thinking like that) – vinod Dec 23 '11 at 12:49
  • It has to be in the same web application(war) that you test. It won't affect your coverage statistics, it just triggers writing of the coverage data. – oers Dec 23 '11 at 12:51
  • thanks for the help oers.by inserting piece of code i got the report but my questions now is if u running one test case and produce the report(with out stopping tomcat) now i want to flush the coverage.ser to 0% and execute another test case(here i am not stopping tomcat for running other test case)..sorry for my weak english – vinod Dec 26 '11 at 09:07
  • you have to delete the cobertura.ser, but I'm not sure if this will work (it may include the first test) – oers Dec 26 '11 at 09:30
  • 1
    what is the use of coberturaflush.war file – vinod Dec 26 '11 at 10:02
  • i am making a jar file of project1 and placing the jar file in project2.for project2 i am able to creating the coberatura report.my question is can get cobertura report for project1 (which is in jar) while running proejct2?can we ge cobertura report for jar files? jar file is important for project2 otherwise project will not run – vinod Feb 02 '12 at 18:07
  • the link for above question is here http://stackoverflow.com/questions/9117739/cobertura-report – vinod Feb 02 '12 at 18:27