Your most unhappy customers are your greatest source of learning.

jacoco code coverage with Ant - Sonar

Here is the detail for how to setup code coverage using jacoco plugin which is OOB come out with SONAR :

  1. Write sample java project called ant-jacoco-codecoverage
  2.  Project folder structure would be given as below snapshot:
  3. Under the target folder you may have to create classes & reports folders where reports folder will have one more folder called junit
  4. Write sample Java class One.java in src folder you can copy paste below code  :
    1. public class One {
        String message = "foo";
        public String foo() {
          return message;
        }
        public void uncoveredMethod() {
          System.out.println(foo());
        }
      }
  5. Now create test class called OneTest.java and put the below code :
    1. import static org.junit.Assert.assertEquals;
      import org.junit.Test;
      public class OneTest {
        @Test
        public void testFoo() throws Exception {
          One one = new One();
          assertEquals("foo", one.foo());
         }
         }
      }
       
  6. Now put the junit-4.10.jar file under lib folder
  7. Download jacocoant.jar  jacoco ant task library and put that jar file under Apache Ant /lib directory
  8. Now create build.xml to run the test cases and generate sonar report (Use below code)
    1. <?xml version="1.0" encoding="UTF-8"?>
      <project name="UT coverage with Ant and JaCoCo running tests" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">

          <!-- ========= Define the main properties of this project ========= -->
          <property name="src.dir" value="src" />
          <property name="test.dir" value="test" />
          <property name="lib.junit.dir" value="lib" />
          <property name="build.dir" value="target" />
          <property name="classes.dir" value="${build.dir}/classes" />
          <property name="reports.dir" value="${build.dir}/reports" />
          <property name="reports.junit.xml.dir" value="${reports.dir}/junit" />
         
          <!-- Define the Sonar properties -->
         
          <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonar332?useUnicode=true&amp;characterEncoding=utf8" />
              <property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />

              <!-- the username and password of your database -->
              <property name="sonar.jdbc.username" value="root" />
              <property name="sonar.jdbc.password" value="password" />

          <property name="sonar.projectKey" value="org.codehaus.sonar:example-ut-ant-jacoco-runTests" />
          <property name="sonar.projectName" value="UT coverage with Ant and JaCoCo running tests" />
          <property name="sonar.projectVersion" value="1.0" />
          <property name="sonar.language" value="java" />
          <property name="sonar.sources" value="${src.dir}" />
          <property name="sonar.tests" value="${test.dir}" />
          <property name="sonar.binaries" value="${classes.dir}" />
          <property name="sonar.sourceEncoding" value="UTF-8" />
          <property name="sonar.surefire.reportsPath" value="${reports.junit.xml.dir}" />  

          <!-- The following properties are required to use JaCoCo: -->
          <property name="sonar.dynamicAnalysis" value="reuseReports" />
          <property name="sonar.java.coveragePlugin" value="jacoco" />
          <property name="sonar.jacoco.reportPath" value="target/jacoco.exec" />

       
          <!-- ========= Define "regular" targets: clean, compile, test, ... ========= -->
          
    2. <target name="clean">
              <delete dir=".sonar" />
              <delete dir="${build.dir}" />
              <delete dir="${reports.dir}" />
          </target>

          <target name="init">
              <mkdir dir="${build.dir}" />
              <mkdir dir="${classes.dir}" />
              <mkdir dir="${reports.dir}" />
              <mkdir dir="${reports.junit.xml.dir}" />
          </target>

          <target name="compile" depends="init">
              <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"  />
              <javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" />
          </target>

             
          <path id="classpath">
              <fileset dir="${lib.junit.dir}" includes="*.jar"/>
          </path>
         
          <target name="test" depends="compile">
         
              <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
                  <classpath>
                      <path refid="classpath"/>
                  </classpath>
              </taskdef>
         
              <!-- Import the JaCoCo Ant Task -->
              <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
                 
              </taskdef>
         
              <!-- Run your unit tests, adding the JaCoCo agent -->
              <jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
                <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
                  <classpath location="${classes.dir}" />
                  <classpath refid="classpath" />

                  <formatter type="xml" />
                  <batchtest todir="${reports.junit.xml.dir}">
                    <fileset dir="${test.dir}">
                      <include name="**/*Test.java" />
                    </fileset>
                  </batchtest>
                </junit>
              </jacoco:coverage>   
             
          </target>

          <!-- ========= Define Sonar target ========= -->
          <target name="sonar" depends="compile">
              <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
                  <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
                  <classpath path="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" />
              </taskdef>
             
              <!-- Execute Sonar -->
               <sonar:sonar key="${sonar.projectKey}" version="0.1-SNAPSHOT"  xmlns:sonar="antlib:org.sonar.ant"/>
          </target>

          <!-- ========= The main target "all" ========= -->
          <target name="all" depends="clean,compile,test,sonar" />

      </project>
  9. Now start sonar server
  10. Run  >> ant compile
  11. ant test
  12. ant sonar
  13. Now if all above commands running properly, then see the code coverage in sonar dashboard
    1. See below snapshot:
    2.  
Note :
For Sonar below version than 3.4, you may have to set below property for coverage:
<property name="sonar.code.codeCoveragePlugin" value="jacoco" />
Instead of
<property name="sonar.java.coveragePlugin" value="jacoco" />

1 comment

  1. I tired the above Example, i'm facing error while making ant build..

    BUILD FAILED
    C:\Users\FL703\workspace\ant-jacoco-codecoverage\build.xml:77: Problem: failed to create task or type antlib:org.jacoco.ant:coverage
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any / declarations have taken place.
    No types or tasks have been defined in this namespace yet

    This appears to be an antlib declaration.
    Action: Check that the implementing library exists in one of:
    -C:\Users\FL703\Downloads\Liferay\eclipse_Liferay_IDE_1.5.0_v201201102224-win32-x86_64\eclipse\plugins\org.apache.ant_1.8.2.v20110505-1300\lib
    -C:\Users\FL703\.ant\lib
    -a directory added on the command line with the -lib argument


    How to resolve this.....

    ReplyDelete

Most Reading

 

Like Me & Share

Buy Websites PRchecker.info

Members

Ranks