0

I am trying to do the code coverage using Jacoco. I have written the Jacoco configuration in ANT script.Jacoco working fine without logs in code. But it is error out for loggers (apache log4j) added in my code.

I tried to exclude the logs by adding below code in my ant script as <jacoco:coverage destfile="${result.exec.file}" excludes="org.apache.log4j.*"> still it is giving me same error.

I am getting below exception:


    15:35:16     [junit] Testcase: testUncoveredMethod took 0.009 sec
    15:35:16     [junit]    Caused an ERROR
    15:35:16     [junit] org/apache/log4j/Category
    15:35:16     [junit] java.lang.NoClassDefFoundError: org/apache/log4j/Category
    15:35:16     [junit]    at com.ant.sonar.jacoco.junit.code.coverage.JacacoCoverage.<clinit>(JacacoCoverage.java:6)
    15:35:16     [junit]    at com.ant.sonar.jacoco.junit.code.coverage.JacacoCoverageTest.testUncoveredMethod(JacacoCoverageTest.java:23)
    15:35:16     [junit] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Category
    15:35:16     [junit]    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    15:35:16     [junit]    at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
    15:35:16     [junit]    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    15:35:16     [junit]    at java.lang.ClassLoader.loadClass(ClassLoader.java:352)

Jacoco configuration

<target name="test" depends="compile">
<jacoco:coverage destfile="${result.exec.file}" excludes="org.apache.log4j.*">
<junit showoutput="true" printsummary="on" enabletestlistenerevents="true" fork="true" haltonfailure="no" forkmode="once">
<classpath path="${result.classes.dir}"/>
<classpath path="${junit.jar.path}"/>
<classpath path="${hamcrest.jar.path}"/>
<classpath location="../../ant-junit-1.9.5.jar"/>
<formatter type="plain" usefile="false"/>
<test name="com.ant.sonar.jacoco.junit.code.coverage.JacacoCoverageTest"/>
</junit>
</jacoco:coverage>
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!--
 This task needs the collected execution data and ... 
-->
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}"/>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}"/>
<csv destfile="${result.report.dir}/report.csv"/>
<xml destfile="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>

1 Answers1

0

I got the fix for this issue. Need to add log4j jar in junit classpath