Code Coverage for Free: How to Use EMMA
Execute Your Example
Now that you have recompiled, instrumented, and copied class files to the classes directory, you can start executing your application. In this step, you will call your application several times. As I mentioned earlier, your test application consists of one servlet, EmmaTestServlet.java, and one jsp, input.jsp. The servlet accepts two parameters from the JSP and, based on these parameters, returns true or false. Repeat the same steps you saw at the beginning of the article:
- Parameter 1 = 1, Parameter 2 = 0
- Parameter 1 = 0, Parameter 2 = 1
- Parameter 1 = 2, Parameter 2 = 2
Extract Coverage Dump
Locate the 4_emma-extract-covarage.xml ANT Script and execute it. By using the <CTL> command, this script communicates with a JVM running EMMA-instrumented classes via a TCP socket. It downloads current coverage data from a JVM at a given TCP address and saves it into a local file, 'mycoverage.ec'. The output of the script should look like this:
Locate the 5_emma_report.xml ANT Script and execute it. This script executes a <report> EMMA command- offline coverage report generator. It reads in a 'metadata.em' class metadata (produced by the instrumentation step) and runtime 'mycoverage.ec' coverage data (produced by extracting the coverage dump step) and generates a coverage report. The execution output should look like this:
You finally can open the generated index.html file in the browser to review your results. The report shows that 100% of your code has been executed.
Figure 10 (overall coverage summary)
Figure 11 (coverage summary for source file [EmmaTestServlet.java])
The example that is explained above reveals only a part of EMMA's full functionality, but I hope it will help you get started with this great framework. The framework provides many customization options that you can read about in the help files provided by the author of the framework.
Download the source code for this article here.
About the Author
Aleksey Shevchenko has been working with object-oriented languages for over seven years. For the past four years, he has served as a technical lead and a project manager. Aleksey has been implementing Enterprise IT solutions for Wall Street and the manufacturing and publishing industries.
Page 2 of 2