Tuesday, April 25, 2017

Structural Code Coverage For Java With NetBeans

Task
Structural Code Coverage
Tools / Software
JaCoCo, NetBeans, JUnit
Download Link
plugins.netbeans.org/plugin/48750/tikione-jacocoverage
Prerequisites
Netbeans IDE, JUnit libraries, Knowledge of unit testing, OOP and JUnit.
Description
TikiOne JaCoCoverage is a plugin for NetBeans IDE which provides JaCoCo code coverage.

Installation

Go to the download link provided in the above table and download the plugin for your already installed NetBeans version. Extract the downloaded archive to a folder. Open NetBeans IDE. From Tools menu select Plugins. From the Plugins window go to Downloaded tab and add all the 4 .nbm files from the extracted folder and finally click Install. After accepting license agreements IDE will prompt you to restart. Select restart. IDE will restart.

What is Structural Code Coverage?

Structural Code Coverage is a measurement of no. of lines / blocks / segments executed for a given test case.

JaCoCo

JaCoCo is an open source toolkit for measuring and reporting for Java Code Coverage. There are many implementations of JaCoCo (Jenkins JaCoCo, SonarQube JaCoCo etc.). Some distinct features of JaCoCo are,
  • ·         Provides line and branch coverage.
  • ·         We can save the output to a file.
  • ·         Supports recent Java versions.
  • ·         Inspects Java byte code.
  • ·         Easy to execute code coverage tests.
  • ·         Supports many popular IDEs and tools (NetBeans, Eclipse, SonarCube, Jenkins etc.)

There are few disadvantages of JaCoCo and structural code coverage,

  • ·         Code coverage does not reflects effective testing. There are more sophisticated ways for testing in the code level.
  • ·         JaCoCo needs pre written JUnit unit test cases to perform code coverage tests. This is an overhead.
  • ·         Core implementation of JaCoCo has not been updated since long time. There are more modern code coverage tools like CheckMarx used in industrial levels.
Structural Code Coverage Using JaCoCo Plugin for NetBeans IDE
Create an empty Java project. Add a simple class and add a method with some if-else conditions. So we can test branch coverage too. Now create a unit test case for this method using JUnit. After writing the JUnit class check whether you are getting some output from the JUnit test case.

Now left click on your NetBeans project and click Test with JaCoCoverage. Your source code will be highlighted and test result will be opened in your web browser as follows.

Green colored are the code lines which were covered during the test and red colored lines are the code lines which were not covered. Yellow colored lines are lines which were partially covered. Refer line no. 17. Only the true part of the branch has been covered. Yellow colored diamond shows this situation. In line no. 20, neither of atomic conditional statements are not covered. Red colored diamond reflects this situation.

In opened web page you can see a summary of the test. The above gives information about total no. of lines, conditional statements and their covered percentage. Try above unit test with different input values to get different results.

Reviews

  • ·         The plugin discussed in this article is a very nice tool to check code coverage in the Eclipse IDE. I did not talk about how to use it in the Eclipse IDE, because it is very intuitive and very easy to use. - Dr. Song Li – The Code Project
  • ·         Idea is pretty simple. Application is started with a code coverage tool is attached to it, any test are executed and results are gathered.  - Lyudmil Latinov – Automation Rhapsody


Group Details

Batch: Software Engineering (2015), Weekend

IT 15 0172 84
M.M.M.S. Rupasinghe
IT 15 0190 28
L.T. Marasinghe
IT 15 0331 92
T.T.C. Phillips




No comments:

Post a Comment