0
votes

I'm using Jacoco code coverage in my Ant build and build is success after using instrument for my junit classes as they have powermockito in it.
I'm getting errors in the junit classes when i ran through the ant and tests failed but build success. I'm comipiling the src and test classes using WAS server jars from another build.xml and placed the class files in the dir.build folder. This folder is given as input for the instrument task to give me the instrumented classes. When the junit task is started it is saying that errors occured in the test classes and test failed. I used the printsummary ="on" and tried to generate logs using verbose.
I want to know what are the errors occuring while running the junit. Can somebody please tell me how can i see the errors in the Test classes.

<?xml version="1.0" encoding="UTF-8"?>
<project name = "JunitIntegration" default = "report" xmlns:jacoco="antlib:org.jacoco.ant">

    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="./ant/lib/jacocoant.jar"/>
    </taskdef>


    <property name= "project.name" value = "Services"/>
    <property name= "source.dir" value = "C:\\Thejesh\\Workspaces\\firstSetup\\Services"/>
    <property name= "dir.build" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\build\\Services-classes"/>

    <!-- Code coverage report -->
    <property name= "result.dir" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\junitResults"/>
    <property name= "result.classes.instr.dir" value = "${result.dir}/classes-instr"/>
    <property name= "result.report.dir" value = "${result.dir}/site/jacoco"/>
    <property name= "result.exec.file" value = "${result.dir}/jacoco.exec"/>

    <import file="build-utils.xml"/>    

    <target name ="cleanL">
        <delete dir ="${result.dir}"/>
    </target>

    <target name ="createL">
        <mkdir dir ="${result.dir}"/>
        <mkdir dir ="${result.classes.instr.dir}"/>
    </target>



    <target name ="instrument" depends = "cleanL, createL">
        <jacoco:instrument destdir="${result.classes.instr.dir}">
            <fileset dir="${dir.build}"/>
        </jacoco:instrument>
    </target>


    <target name ="test" depends = "instrument">
        <record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="start"/>
        <jacoco:coverage destfile="${result.exec.file}" xmlns:jacoco="antlib:org.jacoco.ant"
            exclclassloader = "sun.reflect.DelegatingClassLoader">
            <junit fork="true" forkmode="once" printsummary="on">
                <formatter type= "xml"/>
                <classpath>
                    <pathelement path="${dir.build}" />
                    <pathelement path="./ant/lib/jacocoagent.jar" />
                    <fileset dir ="C:\\Thejesh\\PIF\\Jars\\Common">
                        <include name ="*.jar*" />
                    </fileset>
                </classpath> 

                <batchtest todir ="${result.dir}">
                    <fileset dir= "${result.classes.instr.dir}">
                        <include name ="**/*Test.class*"/>
                    </fileset>
                </batchtest>
            </junit>
        </jacoco:coverage>
        <record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="stop"/>

    </target>

    <!-- Generating code coverage reports -->
    <target name ="report" depends ="test">
        <jacoco:report>
            <executiondata>
                <file file="${result.exec.file}"/>
            </executiondata>

            <structure name="JUnit intergration report">
                <classfiles>
                    <fileset dir="${dir.build}"/>
                </classfiles>
                <sourcefiles encoding="UTF-8">
                    <fileset dir="${source.dir}"/>
                </sourcefiles>
            </structure>

            <html destdir="${result.report.dir}"/>
            <xml destfile ="${result.report.dir}/report.xml"/>

        </jacoco:report>
    </target>

    <target name ="compiler" >
        <junit.compile
            projectdir="${source.dir}"
            destfile="${dir.build}"
        />
    </target>



</project>
1
Is this junit or jacoco issue? If you comment out the jacoco tags, does the junit work properly? - DuncG
I think its Junit because I tried to run the junit by commenting the jacoco tags and still got the same issue. - Thejesh Bommisetty
Have you tried haltonfailure="true" and haltonerror="true" in the junit tag to make it bail out early? - DuncG
Its not about stopping the build when errors come, I want to find what the errors are while running - Thejesh Bommisetty

1 Answers

0
votes

One way that may help you. Hopefully someone can suggest an easier way - this is snippet from build.xml I used a few years ago before upgrade to JUNIT5.

<junit printsummary="on" showoutput="off" fork="true"
      haltonerror="false" haltonfailure="false"
      errorproperty="junit.errors" failureproperty="junit.failures"  >
  <formatter type="plain"/>
  <formatter type="xml"/>

You'd need to check what outout files you get from JUNIT4 in your ${result.dir} and add ant action to grab and print out part of the junit output files to the console so that the final error lines were visible

<loadfile srcfile="${junit.stdout}" property="junit.summary">
      <filterchain><tailfilter lines="30"/></filterchain>
</loadfile>

<echo>${junit.summary}</echo>

and then bail out of the build if there are errors:

<fail if="junit.failures">JUNIT failed - see ${result.dir}</fail>
<fail if="junit.errors"  >JUNIT error - see ${result.dir}</fail>