Cobertura + Junit - java

I have a question about cobertura in a connection with Ant scripts. I have already read these two articles on StackOverflow:
SONAR - Measure Code Coverage using Cobertura and Bamboo + sonar.dynamicAnalysis=reuseReports = 0% Rules Compliance
Furthermore, I have searched in the internet for a solution for my problem.
Now I hope you can help me.
The initial situation:
I have an Ant script to compile a library and then a test target. Now I have added cobertura. After some time I got it out that the classes and also the ser file is stored. The report is also generated, but with no coverage.
But now the problem is that the JUnit test will not work. The following error message can be found in the xml:
<testcase classname="com.***.***.***.***.***Test" name="test16" time="0.0">
<error message="com/***/***/***/***/***Rule" type="java.lang.NoClassDefFoundError">java.lang.NoClassDefFoundError: com/***/***/***/***/***Rule
at com.***.***.***.***.***Test.setUp(Unknown Source)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
I currently use the following code in the Ant script:
<project name="MainLIB" default="vbuild" basedir="./">
<path id="path.additionaltasks">
<fileset dir="../BUILD/antlib">
<include name="cobertura.jar" />
<include name="log4j-1.2.9.jar" />
<include name="asm-3.0.jar" />
<include name="asm-tree-3.0.jar" />
<include name="jakarta-oro-2.0.8.jar" />
</fileset>
</path>
<taskdef resource="tasks.properties" classpathref="path.additionaltasks" />
<path id="path.LIB">
<fileset dir="lib" includes="**/**.jar" />
</path>
<path id="path.UNITTEST">
<path refid="path.LIB" />
<pathelement path="build/cobertura/classes" />
<pathelement path="build/classes" />
<pathelement path="build/test-classes" />
</path>
<property name="dir.build" value="build" />
<property name="dir.build.classes" value="${dir.build}/classes" />
<property name="dir.build.cobertura" value="${dir.build}/cobertura" />
<property name="dir.build.cobertura.classes" value="${dir.build.cobertura}/classes" />
<property name="dir.build.test.classes" value="${dir.build}/test-classes" />
<property name="dir.build.test.report" value="${dir.build}/test-reports" />
<property name="dir.build.dist" value="${dir.build}/dist" />
<target name="vbuild" depends="build,test" description="Build and run tests" />
<target name="build" description="Compile classes and create .jar file in build/dist">
<delete dir="${dir.build}" />
<mkdir dir="${dir.build.classes}" />
<javac srcdir="src" classpathref="path.LIB" verbose="false" destdir="${dir.build.classes}" includeantruntime="false" includejavaruntime="false" debug="true">
</javac>
<mkdir dir="${dir.build.dist}" />
<jar jarfile="${dir.build.dist}/${jar.name}.jar" basedir="${dir.build.classes}">
</jar>
</target>
<target name="test" description="Compile and Run jUnit Tests">
<mkdir dir="${dir.build.test.classes}" />
<mkdir dir="${dir.build.test.report}" />
<javac srcdir="test" classpathref="path.UNITTEST" verbose="false" destdir="${dir.build.test.classes}" includeantruntime="false" includejavaruntime="false">
</javac>
<delete dir="${dir.build.cobertura}" />
<mkdir dir="${dir.build.cobertura}" />
<mkdir dir="${dir.build.cobertura.classes}" />
<cobertura-instrument todir="${dir.build.cobertura.classes}" datafile="${dir.build.cobertura}/cobertura.ser">
<fileset dir="${dir.build.classes}" />
</cobertura-instrument>
<junit printsummary="yes">
<classpath>
<!--<pathelement path="build/cobertura/classes" />-->
<path refid="path.UNITTEST" />
</classpath>
<formatter type="xml" />
<batchtest todir="${dir.build.test.report}">
<fileset dir="${dir.build.test.classes}">
<include name="**/*Test.class" />
</fileset>
</batchtest>
</junit>
<cobertura-report format="xml" datafile="${dir.build.cobertura}/cobertura.ser" destdir="${dir.build.cobertura}/" />
<cobertura-report format="html" datafile="${dir.build.cobertura}/cobertura.ser" destdir="${dir.build.cobertura}/" />
</target>
The problem is, the classes are in the build/cobertura/classes directory. And I can't figure out, why Junit, can't find the classes

Your classpath:
<classpath>
<!--<pathelement path="build/cobertura/classes" />-->
<path refid="path.UNITTEST" />
</classpath>
Contains:
<path id="path.LIB">
<fileset dir="lib" includes="**/**.jar" />
</path>
<path id="path.UNITTEST">
<path refid="path.LIB" />
<pathelement path="build/cobertura/classes" />
<pathelement path="build/classes" />
<pathelement path="build/test-classes" />
</path>
But it also needs the cobertura jars, which you reference in path.additionaltasks. Cobertura instruments the class files with byte code manipluation. Those files still need the cobertura.jar dependency to work.
To fix it add the path.additionaltasks to junit:
<classpath>
<!--<pathelement path="build/cobertura/classes" />-->
<path refid="path.UNITTEST" />
<path refid="path.additionaltasks" />
</classpath>

Related

Build Successfully but not running junit files using Apache Ant

I am new in Apache ant to build the project. In my Project i used MySQL,junit and hibernate library. I am working on bitbucket pipelines so some suggest use to ant. So i am creating the build.xml file and in there the proecess is clean,build,run junit test case. I did many research how to learn these things but i am stuck in some place. I am success in clean the directory and build also but i am stuck to check junit failure test case. I tried to run these test case in eclipse it shows that test case fail but if i run through ant it always say build successfully. I will provide my project structure .
+HiberTest
--HTest
--lib
--mysql
--junit
-- Hibernate..... so many library
--src
-com
-test
**so many junit files**
-com
-utils
**so many files**
--classpath
--.settings
--.project
**and many more files**
--build.xml
Here is my build.xml files
<project basedir="." default="junit" name="Hibernate test">
<property name="debuglevel" value="source,lines,vars" />
<property name="src.dir" location="HTest/src" />
<property name="build.dir" location="HTest/bin" />
<property name="target" value="1.8" />
<property name="source" value="1.8" />
<property name="test.dir" value="HTest/bin/com/test" />
<path id="classpath">
<pathelement location="bin" />
<pathelement location="HTest/lib/junit3.8.1.jar" />
<pathelement location="HTest/lib/antlr-2.7.7.jar" />
<pathelement location="HTest/lib/byte-buddy-1.10.7.jar" />
<pathelement location="HTest/lib/classmate-1.5.1.jar" />
<pathelement location="HTest/lib/dom4j-2.1.1.jar" />
<pathelement location="HTest/lib/FastInfoset-1.2.15.jar" />
<pathelement location="HTest/lib/hibernate-commons-annotations-5.1.0.Final.jar" />
<pathelement location="HTest/lib/hibernate-core-5.4.11.Final.jar" />
<pathelement location="HTest/lib/istack-commons-runtime-3.0.7.jar" />
<pathelement location="HTest/lib/jandex-2.1.1.Final.jar" />
<pathelement location="HTest/lib/javassist-3.24.0-GA.jar" />
<pathelement location="HTest/lib/javax.activation-api-1.2.0.jar" />
<pathelement location="HTest/lib/javax.persistence-api-2.2.jar" />
<pathelement location="HTest/lib/jaxb-api-2.3.1.jar" />
<pathelement location="HTest/lib/jaxb-runtime-2.3.1.jar" />
<pathelement location="HTest/lib/jboss-logging-3.3.2.Final.jar" />
<pathelement location="HTest/lib/jboss-transaction-api_1.2_spec-1.1.1.Final.jar" />
<pathelement location="HTest/lib/mysql-connector-java-5.1.48.jar" />
<pathelement location="HTest/lib/stax-ex-1.8.jar" />
<pathelement location="HTest/lib/txw2-2.3.1.jar" />
</path>
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="init" depends="clean">
<mkdir dir="${build.dir}" />
<copy includeemptydirs="false" todir="${build.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.launch" />
<exclude name="**/*.java" />
</fileset>
</copy>
</target>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}" />
<javac debug="true" debuglevel="${debuglevel}" destdir="${build.dir}" includeantruntime="false" source="${source}" target="${target}">
<src path="${src.dir}" />
<classpath refid="classpath" />
</javac>
</target>
<target name="junit" depends="build-project">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="classpath" />
<pathelement location="HTest/src/com/utils" />
</classpath>
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${test.dir}" includes="*.java" />
</batchtest>
</junit>
</target>
</project>
It always gives me this output:-
clean:
[delete] Deleting directory F:\Repository\Agile\HiberTest\HTest\bin
init:
[mkdir] Created dir: F:\Repository\Agile\HiberTest\HTest\bin
[copy] Copying 1 file to F:\Repository\Agile\HiberTest\HTest\bin
build-project:
[echo] Hibernate test: F:\Repository\Agile\HiberTest\build.xml
[javac] Compiling 3 source files to F:\Repository\Agile\HiberTest\HTest\bin
junit:
BUILD SUCCESSFUL
Total time: 1 second
I want to check that if junit test fail then stop the build process and if passes all without error and then i want to create executable jar files
Thanks in advance
The build starts in "build" and the hierarchical never call junit. Try to change:
<target depends="build-project" name="junit" />

Power Mock Code Coverage using Corbetura, Ant 1.8.2 with JDK 1.5

I have searched for how to get coverage report for Corbetura for Power Mock Test Cases.
Found many answers for that, but still getting many issues.
I am using below configuration in my application:
Java (JDK) : 1.5.0_22
Eclipse : Indigo
Ant : 1.8.2
Corbetura Libraries : Tried with 2.1.1
(Still question which set of libraries to use ?)
Please help me on this.
Note: I am using JDK 1.5, I have found many links where it has mentioned configurations with JDK 1.6.
One more thing:
As of now I am not able to execute Ant script with <cobertura-instrument> tag.
Find below the build XML I am using.
I have removed some portion of build xml (I cannot share that due to confidentiality).
Please suggest what is the correct way, to do the code coverage report of Power Mock Test Cases using Corbetura and JDK 1.5 with Ant.
<project name="projName" basedir="." default="clean">
<description>
Project Desc
</description>
<!-- PROPERTIES -->
<property name="src.dir" location="src" />
<property name="test.dir" location="test" />
<property name="build.dir" location="build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="testclasses.dir" value="${build.dir}/testclasses" />
<property name="instrumentedclasses.dir" value="${build.dir}/instrumentedclasses" />
<property name="dist.dir" location="dist" />
<property name="resources.dir" location="resources" />
<property name="libs.dir" value="lib" />
<property name="testReport.dir" location="testReport" />
<property name="cobertura.reports.dir" location="codecoverage" />
<property name="cobertura.datafile" value="${build.dir}/cobertura.ser" />
<property name="project.name" value="${ant.project.name}" />
<property name="cobertura.dir" value="D:\\corbetura_libs" />
<!-- PATHS -->
<path id="src.compile.classpath" description="classpath for compiling sources">
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
<exclude name="**/junit*.jar" />
</fileset>
</path>
<path id="test.compile.classpath" description="classpath for compiling test-sources">
<pathelement location="${classes.dir}" />
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
<exclude name="**/cobertura-2.1.1.jar" />
<exclude name="cobertura-dependencies/*.jar" />
</fileset>
</path>
<path id="test.runtime.classpath" description="classpath for running junit-tests">
<pathelement location="${testclasses.dir}" />
<pathelement location="${instrumentedclasses.dir}" />
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
<exclude name="**/cobertura-2.1.1.jar" />
<exclude name="cobertura-dependencies/*.jar" />
<exclude name="cobertura-dependencies/hamcrest*.jar" />
</fileset>
</path>
<path id="cobertura.classpath" description="classpath for test-report">
<fileset dir="${cobertura.dir}">
<include name="cobertura*.jar" />
<include name="cobertura-dependencies/*.jar" />
</fileset>
</path>
<!-- taskdefs -->
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<!-- PREPARE -->
<target name="prepare" description="creating the needed directories for build and test report">
<delete dir="${build.dir}" />
<delete dir="${testReport.dir}" />
<delete dir="${cobertura.reports.dir}" />
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${testclasses.dir}" />
<mkdir dir="${cobertura.reports.dir}" />
<mkdir dir="${testReport.dir}" />
</target>
<!-- COMPILE SOURCES -->
<target name="compile.sources" depends="prepare" description="compile the source ">
<!-- Compile the java code from ${src.dir} into ${build.dir} -->
<javac classpathref="src.compile.classpath" srcdir="${src.dir}" includeantruntime="false" source="1.5" target="1.5" destdir="${classes.dir}" debug="true" />
<copy todir="${classes.dir}">
<fileset dir="${resources.dir}" includes="*.properties" />
</copy>
</target>
<!-- CORBETURA INSTRUMENT CLASSES -->
<target name="instrument.classes" depends="compile.sources">
<cobertura-instrument todir="${instrumentedclasses.dir}" datafile="${cobertura.datafile}">
<includeclasses regex=".*" />
<instrumentationClasspath>
<pathelement location="${classes.dir}" />
</instrumentationClasspath>
</cobertura-instrument>
</target>
<!-- COMPILE TEST CASES -->
<target name="compile.tests" depends="instrument.classes" description="compile test classes">
<javac classpathref="test.compile.classpath" srcdir="${test.dir}" source="1.5" target="1.5" destdir="${testclasses.dir}" debug="true" includeantruntime="false" />
<copy todir="${testclasses.dir}">
<fileset dir="${resources.dir}" includes="*.properties" />
</copy>
</target>
<!-- RUN TEST CASES -->
<target name="run.tests" depends="compile.tests" description="execute junit test cases">
<junit haltonfailure="false" printsummary="yes" showoutput="yes" fork="true">
<!-- junit-specific -->
<formatter type="xml" />
<classpath refid="test.runtime.classpath" />
<batchtest fork="yes" todir="${testReport.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
<!-- cobertura-specific -->
<sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.datafile}" />
<classpath refid="cobertura.classpath" />
</junit>
<cobertura-report format="html" destdir="${cobertura.reports.dir}" datafile="${cobertura.datafile}" srcdir="${src.dir}" />
</target>
<!-- CREATE TEST CASE REPORT -->
<target name="report" depends="run.tests">
<junitreport todir="${testReport.dir}">
<fileset dir="${testReport.dir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${testReport.dir}" />
</junitreport>
</target>
</project>

Ecplipse ANT build error

I'm new with ANT build, and I have a trouble with it, when I try to build I'm receiving a message:
BUILD FAILED
C:\Users\user\Documents\PM\proyecto-base\proyecto-base\intranet\build.xml:169: Reference xdoclet.classpath not found.
but I have xdoclet.classpath declared already in the same file.
Also I have the properties like ${xdoclet.lib.dir} declared in a build.properties file.
here's my build.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Ant Build File para Intranet -->
<project name="${project.name}" default="ear" basedir=".">
<!-- Importar funcionalidad comun -->
<import file="../common.xml" />
<property name="jboss.security-domain" value="java:/jaas/NotificacionApplicationSecurity" />
<target name="init">
<path id="classpath">
<fileset dir="${lib.dir}" />
<fileset dir="${hibernate.dir}" />
<fileset dir="${hibernate-annotations.dir}" />
<fileset dir="${sri-adm.dir}" />
<fileset dir="${sri-seguridad.dir}" />
<fileset dir="${jsf.dir}" />
<fileset file="${servicio.jar}" />
<fileset file="${log4j.jar}" />
<fileset file="${servlet.jar}" />
<fileset file="${mail.jar}" />
<fileset dir="${jboss.lib}">
<include name="*.jar" />
</fileset>
</path>
<path id="xdoclet.classpath">
<fileset dir="${xdoclet.lib.dir}" />
<pathelement location="${j2ee.jar}" />
<pathelement location="${servlet.jar}" />
</path>
</target>
<target name="clean" depends="init">
<delete dir="../${build.dir}/intranet" />
<delete dir="../${dist.dir}">
<exclude name="${servicio.name}-${version}.jar"/>
</delete>
</target>
<!--=============== Compile JAVA files ========================-->
<target name="compile" description="Compile" >
<echo message="Start compilation..." />
<!-- compile project-->
<mkdir dir="../${build.intranet.dir}/classes"/>
<javac destdir="../${build.intranet.dir}/classes" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
<src path="${src.main.java.dir}" />
<classpath refid="classpath" />
</javac>
</target>
<!--============== Genera archivos web==========-->
<target name="webdoclet" description="Generar archivos XML Web y Struts" >
<echo message="Generando Web" />
<delete file="../${metadata.intranet.web.dir}/jboss-web.xml"/>
<webdoclet destdir="../${metadata.intranet.web.dir}" force="${xdoclet.force}" mergedir="${conf.xdoclet.intranet.dir}">
<fileset dir="${src.main.java.dir}">
<include name="**/*.java" />
</fileset>
<deploymentdescriptor displayname="SRI" description="" xmlencoding="ISO-8859-1"
validatexml="true" servletspec="2.4" sessiontimeout="60" destdir="../${metadata.intranet.web.dir}"
mergeDir="../${conf.xdoclet.intranet.dir}" distributable="false" />
<facesconfigxml destinationfile="${project.name}-faces-config.xml" mergedir="../${metadata.intranet-jsf}" />
<jbosswebxml
version="4.0"
mergeDir="${metadata.intranet-jbossweb}"
contextroot="${project.name}"
securitydomain="${jboss.security-domain}"
validateXML="true" xmlencoding="ISO-8859-1"/>
</webdoclet>
<replace file="../${metadata.intranet.web.dir}/jboss-web.xml" token="<security-domain" value="<security-domain flushOnSessionInvalidation="true"" />
</target>
<target name="build" depends="init,webdoclet" description="Compila las clases">
<mkdir dir="../${classes.dir}" />
<javac destdir="../${classes.dir}" debug="on" deprecation="on" optimize="off" target="1.5" encoding="ISO-8859-1">
<src path="${src.main.java.dir}" />
<classpath refid="classpath" />
<classpath refid="xdoclet.classpath" />
<!-- Descomentar si se quieren ver los Warnings -->
<!--compilerarg value="-Xlint:unchecked" /-->
</javac>
</target>
<!--============= Genera un archivo WAR del proyecto =============================-->
<target name="war" description="Packages app as WAR" depends="compile, webdoclet">
<echo message="Generando WAR..." />
<mkdir dir="../${dist.dir}/intranet" />
<copy todir="../${metadata.tmp.intranet.dir}">
<fileset dir="../${metadata.intranet.web.dir}" />
<filterset>
<filter token="project.name" value="${project.name}"/>
</filterset>
</copy>
<copy file="../${conf.intranet.dir}/jboss-web.xml" todir="../${metadata.tmp.intranet.dir}">
<filterset>
<filter token="project.name" value="${project.name}"/>
</filterset>
</copy>
<war destfile="../${dist.dir}/intranet/${webapp.intranet.name}.war" webxml="../${metadata.intranet.web.dir}/web.xml">
<classes dir="../${build.intranet.dir}/classes">
<include name="**/**/controladores/*.*"/>
<include name="**/**/web/**/*.*"/>
<include name="**/**/validadores/**/*.*"/>
<include name="**/**/convertidores/**/*.*"/>
<exclude name="**/**/internet/**/*.*"/>
</classes>
<classes dir="../intranet/src/main/java">
<include name="**/**/*.properties"/>
</classes>
<fileset dir="${src.main.webapp.dir}">
<include name="**/*.*" />
</fileset>
<lib dir="${jsf.dir}" >
<include name="rich*.jar" />
<include name="jsf*facelet*.jar" />
</lib>
<lib dir="${sri-seguridad.dir}" >
<include name="seguridad*.jar" />
</lib>
<lib dir="${lib.dir}" >
<include name="commons-digester.jar" />
<include name="commons-beanutils.jar" />
<include name="commons-fileupload.jar" />
<include name="commons-el.jar" />
<include name="commons-lang-2.1.jar" />
</lib>
<!-- Archivos de configuracion en WEB-INF -->
<webinf file="../${metadata.tmp.intranet.dir}/web.xml" />
<webinf file="../${metadata.tmp.intranet.dir}/${project.name}-faces-config.xml" />
<webinf file="../${metadata.tmp.intranet.dir}/jboss-web.xml" />
</war>
<delete dir="../${metadata.tmp.intranet.dir}" />
</target>
<target name="ear" depends="war">
<mkdir dir="../${metadata.intranet-ejb.tmp}" />
<copy todir="../${metadata.intranet-ejb.tmp}">
<fileset dir="../${metadata.intranet-ejb}" />
<filterset>
<filter token="project.name" value="${project.name}"/>
<filter token="intranet.name" value="${intranet.name}"/>
</filterset>
</copy>
<ear destfile="../${dist.dir}/intranet/${webapp.intranet.name}.ear" appxml="../${metadata.intranet-ejb.tmp}/application.xml">
<fileset dir="../${dist.dir}" includes="*.jar"/>
<fileset dir="../${dist.dir}/intranet" includes="*.war"/>
</ear>
<delete dir="../${metadata.intranet-ejb.tmp}" />
</target>
<target name="deploy" depends="ear" description="Copia el archivo WAR en el directorio deploy del server">
<copy file="../${dist.dir}/intranet/${webapp.intranet.name}.ear" todir="${jboss.deploy.intranet.dir}" />
</target>
<!-- DefiniciĆ³n de WebDoclet -->
<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask">
<classpath>
<path refid="xdoclet.classpath" />
<path refid="classpath" />
</classpath>
</taskdef>
<!-- Definicion de JSF -->
<taskdef name="facesconfigxml" classname="xdoclet.modules.jsf.FacesConfigXmlSubTask">
<classpath>
<path refid="xdoclet.classpath" />
<path refid="classpath" />
</classpath>
</taskdef>
</project>
the lines which are returning me the error are the following:
<!-- DefiniciĆ³n de WebDoclet -->
<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask">
<classpath>
<path refid="xdoclet.classpath" />
<path refid="classpath" />
</classpath>
</taskdef>
but as I told you I have already declared my xdoclet.classpath in the same file at the begining:
<path id="xdoclet.classpath">
<fileset dir="${xdoclet.lib.dir}" />
<pathelement location="${j2ee.jar}" />
<pathelement location="${servlet.jar}" />
</path>
Can Anyone help me with this Issue please? thanks a lot
The issue here is that your path definition is inside of the the target "init", but the taskdef definition is outside of any target up at the project level. When ant loads the build file, it's going to try validating everything that is defined at the project level. In this case it fails becuase the init target has not run yet and the xdoclet.classpath path is not defined.
If you just move the path element out to the project level like the taskdef is, the issue should go away.

Loading a resource file for junit works in eclipse but not on any command line?

I have the following code which is called by a junit 4 test class:
final InputStream resourceAsStream = getClass().getResourceAsStream("MyFile.txt");
This works file in eclipse but fails on the command line when running with ant.
Note, I've tried:
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("MyFile.txt"));
and:
Foo.class.getResourceAsStream("MyFile.txt"))
but with no luck.
My test classes, resource file and code are all in the directory:
myproject/tests/src/junit4/org/user/util/services/service
Do I have to do/add something the the build.xml for this to work?
This is the section of my main build.xml which runs the tests:
<target name="junit4" depends="compile-junit4" description="compile and run the junit4 tests">
<ant antfile="${build.rootdir}/make/junit4.xml" dir="${build.rootdir}" target="all" inheritAll="true" inheritRefs="true" />
</target>
And this is the ant file:
<project name="build" default="all" basedir=".">
<target name="common-init">
<property name="junit.tmpdir" value="test-tmpdir" />
<mkdir dir="${junit.tmpdir}" />
<property name="jvm.args" value="-ea -Xms32m -Xmx384m -server -Djava.io.tmpdir=${junit.tmpdir} -Dcom.sun.grizzly.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true" />
<!-- runtime classpath of our junit task -->
<path id="junit4-runtime-classpath">
<path path="${build.rootdir}/external/junit-4.7.jar" />
</path>
<!-- Directory for classes, relative to base directory /-->
<property name="build.classes.dir" value="${build.rootdir}/build/classes/${build.type}" />
<mkdir dir="${build.rootdir}/tests/reports" />
<path id="test.cp">
<!--
uncomment this to use instrumented classes
<pathelement path="${instrumented.dir}"/>
-->
<pathelement path="${build.tool.classes.dir}" />
<pathelement path="${build.classes.dir}" />
<!--
uncomment this to use instrumented classes
<fileset dir="${build.rootdir}/external">
<include name="cobertura/*.jar"/>
</fileset>
-->
<path refid="external.jars.path" />
</path>
<!-- default log.properties to use for all tests -->
<property name="test.log.properties" value="${build.rootdir}/tests/src/unit/log.properties" />
<!-- if set on the command line, pass down the test.propertyFile for global test properties
that are to be used for all tests. should be the name of a Java Properties file
in the test root directory (of each test), or an absolute path that starts with / -->
<property name="test.propertyFile" value="" />
<property name="smoketest.propertyFile" value="" />
</target>
<target name="junit4-init" depends="common-init">
<property name="test.type" value="Junit4" />
<!-- Unit tests java Srcs dir -->
<property name="unit.java.dir" value="tests/src/junit4" />
</target>
<target name="selenium-init" depends="common-init">
<property name="test.type" value="Selenium" />
<!-- Unit tests java Srcs dir -->
<property name="unit.java.dir" value="tests/src/selenium" />
</target>
<property name="tmp.dir" value="${build.rootdir}/build/.tmp.ant" />
<!-- because we are running tests in the test src directory
we need to clean up the transient files.
won't be needed if we were running tests properly in a separate area -->
<target name="clean-testleftovers">
<echo message="Directory is ${build.rootdir}" />
<delete includeemptydirs="true" verbose="true">
<fileset dir="${build.rootdir}/tests/src" erroronmissingdir="true">
<include name="**/systemdb/**/*" />
<include name="**/prefs" />
<include name="**/prefs/*" />
<include name="**/scm/**/*" />
<include name="**/security/**/*" />
<include name="**/security" />
<include name="**/membership/*" />
<include name="**/sequences" />
<include name="**/passwd/*" />
<include name="**/*/*.snapshot" />
<include name="**/sidelinedSchedulers" />
<!-- exclude any java files prefs or log properties that we may have in directories called prefs/scm etc. -->
<exclude name="**/*.java" />
<exclude name="**/pref*.xml" />
<exclude name="**/.privileges.xml" />
<exclude name="**/log.properties" />
</fileset>
</delete>
</target>
<target name="runbatch" depends="clean-testleftovers">
<echo message="Running ${test.type} testcases in batch mode .." />
<mkdir dir="${build.rootdir}/tests/reports" />
<mkdir dir="${build.rootdir}/tests/reports/logs" />
<mkdir dir="${coverage.dir}" />
<!-- clean out old test reports -->
<!-- <delete>
<fileset dir="${build.rootdir}/tests/reports/junit4">
<include name="TEST-*" />
<include name="TESTS*.xml" />
</fileset>
<fileset dir="${build.rootdir}/tests/reports/junit4/logs">
<include name="*.log" />
</fileset>
</delete> -->
<!-- clean out old coverage reports -->
<delete>
<fileset dir="${coverage.dir}">
<include name="*.xml" />
<include name="*.html" />
</fileset>
</delete>
<!-- Timeout individual test in 15 minutes -->
<junit printsummary="withOutAndErr" haltonfailure="no" errorProperty="test.failed" failureProperty="test.failed" timeout="900000">
<jvmarg line="${jvm.args}" />
<!-- <sysproperty key="prefs.file.name" value="${prefs.file.name}"/> -->
<sysproperty key="com.rascal.inprocess" value="true" />
<sysproperty key="base.prefs.root" value="${build.rootdir}/${unit.java.dir}" />
<sysproperty key="build.root.dir" value="${build.rootdir}" />
<sysproperty key="perl.path" value="${perl.path}" />
<sysproperty key="java.io.tmpdir" value="${java.io.tmpdir}" />
<sysproperty key="test.propertyFile" value="${test.propertyFile}" />
<sysproperty key="smoketest.propertyFile" value="${smoketest.propertyFile}" />
<sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.ser}" />
<formatter type="xml" />
<classpath refid="test.cp" />
<batchtest fork="yes" todir="${build.rootdir}/tests/reports">
<fileset dir="${build.rootdir}/${unit.java.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${build.rootdir}/tests/reports">
<fileset dir="${build.rootdir}/tests/reports">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${build.rootdir}/tests/reports" />
</junitreport>
</target>
<target name="coverage-report" if="enable.coverage">
<cobertura-report format="html" datafile="${cobertura.ser}" destdir="${coverage.dir}" srcdir="src/java" />
<cobertura-report format="xml" datafile="${cobertura.ser}" destdir="${coverage.dir}" srcdir="src/java" />
<echo>Coverage report is available at ${coverage.dir}/index.html</echo>
</target>
<target name="all" depends="junit4-init, runbatch">
<fail if="test.failed">
JUnit4 tests failed. Check reports for details
</fail>
</target>
<target name="selenium-tests" depends="selenium-init, runbatch">
<fail if="test.failed">
Selenium tests failed. Check reports for details
</fail>
</target>
</project>
Many thanks!
You should define classpath to your junit runtime in "build.xml".
Could you please paste your "build.xml" content up there ? thanks.

Ant and Junit all tests fail

I need to use ant to test my project develop in Java. I cannot make ant works properly. I haven't trully understood how to set the path for ant which files are needed to test correctly the project.
Which type of file is expecting? source or binary?
I am using this ant file to run the test :
<project name="acmetelecom" default="compile">
<property name="src" location="src/acme/com/acmetelecom" />
<property name="src" location="src/acme/com/acmetelecom" />
<property name="fit" location="fit" />
<property name="bin" location="bin" />
<property name="lib" location="lib" />
<property name="report" location="report" />
<property name="junit.report" location="report/junit" />
<property name="fit.report" location="report/fit" />
<path id="lib.classpath">
<fileset dir="${lib}">
<include name="**/*.jar" />
<include name="*" />
</fileset>
</path>
<path id="test.classpath">
<path refid="lib.classpath" />
<pathelement location="lib/junit/junit-4.4.jar" />
<pathelement location="lib/junit/junit-4.4-src.jar" />
<pathelement location="${bin}" />
</path>
<target name="compile">
<mkdir dir="${bin}" />
<javac srcdir="${src}" destdir="${bin}">
<classpath refid="lib.classpath" />
</javac>
</target>
<target name="junit" depends="compile">
<mkdir dir="${junit.report}" />
<junit printsummary="yes" fork="yes" haltonfailure="no">
<classpath refid="test.classpath" />
<formatter type="plain" />
<batchtest fork="yes" todir="${junit.report}">
<fileset dir="bin/com/acmetelecom">
<include name="**/*Test.class" />
</fileset>
</batchtest>
</junit>
</target>
<target name="fit" depends="compile">
<mkdir dir="${fit.report}" />
<java classname="fitlibrary.runner.FolderRunner" fork="yes"
failonerror="yes">
<classpath refid="test.classpath" />
<arg value="${fit}" />
<arg value="${fit.report}" />
</java>
<echo>Please see the FIT report page for more details</echo>
</target>
<target name="clean">
<delete dir="${bin}" />
<delete dir="${report}" />
</target>
</project>
I cannot see what I am doing wrong! The tests are in the same directory as the source code.
The root node of an Ant script is the project tag. An example I suggest starting simple... like get your Java source to compile, then add junit, etc.
You are writing .class files to ${bin}. Try to give that as argument in batchtest's fileset. There is no need to give packagedirectory paths anyway. */.class is going to check all directories.
<batchtest fork="yes" todir="${junit.report}">
<fileset dir="${bin}">
<include name="**/*Test.class" />
</fileset>
</batchtest>

Categories