Here is the detail for how to setup code coverage using jacoco plugin which is OOB come out with SONAR :
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" />
- Write sample java project called ant-jacoco-codecoverage
- Project folder structure would be given as below snapshot:
- Under the target folder you may have to create classes & reports folders where reports folder will have one more folder called junit
- Write sample Java class One.java in src folder you can copy paste below code :
- public class One {
String message = "foo";
public String foo() {
return message;
}
public void uncoveredMethod() {
System.out.println(foo());
}
} - Now create test class called OneTest.java and put the below code :
- 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());
}
}
}
- Now put the junit-4.10.jar file under lib folder
- Download jacocoant.jar jacoco ant task library and put that jar file under Apache Ant /lib directory
- Now create build.xml to run the test cases and generate sonar report (Use below code)
- <?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&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, ... ========= -->
- <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> - Now start sonar server
- Run >> ant compile
- ant test
- ant sonar
- Now if all above commands running properly, then see the code coverage in sonar dashboard
- See below snapshot:
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" />
I tired the above Example, i'm facing error while making ant build..
ReplyDeleteBUILD 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.....