Can't run Groovy test cases from Ant - ant

My Ant target looks like this:
<target name="junit" depends="compile">
<junit haltonfailure="true" printsummary="true">
<classpath>
<fileset dir="${idea.dir}/lib" includes="junit.jar" />
<fileset dir="${build.dir}" includes="**" />
</classpath>
<formatter type="plain" usefile="true" />
<batchtest fork="false" todir="${out.dir}">
<fileset dir="${build.dir}" includes="test_*/*Test.class" />
</batchtest>
</junit>
</target>
All compiled classes from the project are in ${build.dir}, as well as all compiled test cases. The latter are within ${build.dir}/test_* sub folders.
There is one test class ${build.dir}/test_ecs/EntityManagerTest.class, which apparently is found in batchtest. However, Ant gives me this output in the junit report:
test_ecs.EntityManagerTest
java.lang.ClassNotFoundException: test_ecs.EntityManagerTest
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
Now, what I don't understand is: Why is the test case class file found, but then the error says exactly that class is not found?

try modifying the <classpath>.. section like this:
<junit ..>
<classpath>
<pathelement location="${idea.dir}/lib/junit.jar"/>
<pathelement path="${build.dir}"/>
</classpath>
and see if it runs...

I suspect your issue is the classpath declaration:
<classpath>
<fileset dir="${idea.dir}/lib" includes="junit.jar" />
<fileset dir="${build.dir}" includes="**" />
</classpath>
Are your *.class files located in the build directory? Or a subdirectory of the build dir?
By way of example here's my standard junit task:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${build.dir}/classes"/>
<pathelement path="${build.dir}/test-classes"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${build.dir}/test-reports">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
Note the classpath declaration. It includes the path to 3rd party dependency jars, the directory where I compiled my classes to and the directory I compiled my test classes to.

Related

Jacoco coverage with Ant throws java.lang.instrument.IllegalClassFormatException: Error while instrumenting class

I integrated Jacoco with my Ant build. When I run the build, the test case is executed successfully followed by the below exception in my TEST-com.worker.ManagerTest.xml.
When I add excludes="*" the error is not thrown. But the jacoco.exec is generated with 1kb size and when I run the report nothing is generated. Can someone let me know what am I missing?
Exception:
<![CDATA[java.lang.instrument.IllegalClassFormatException: Error while instrumenting class com/dataaccess/GenericDao.
at org.jacoco.agent.rt_6qyg3i.CoverageTransformer.transform(CoverageTransformer.java:69)
Below is the jacoco build script.
<target name="test" depends="test-compile">
<mkdir dir="${report.dir}" />
<jacoco:coverage destfile="${report.dir}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant" exclclassloader="sun.reflect.DelegatingClassLoader:javassist.Loader">
<junit fork="true" forkmode="once" printsummary="on">
<classpath>
<!--<pathelement location="${basedir}/../../../build/lib/aspectjtools.jar"/>
<pathelement location="${basedir}/../../../build/lib/aspectjrt.jar"/>-->
<pathelement path="${test.path}" />
<pathelement path="${dist.dir}/test/unittest-manager.jar" />
</classpath>
<formatter type="xml" />
<batchtest todir="${report.dir}" fork="yes">
<fileset dir="test">
<include name="**/*Test*" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
</target>
<target name="report" depends="test">
<echo message="Generating Jacoco reports..." />
<property name="report.dir.file" value="${report.dir}/jacoco.exec"/>
<jacoco:report>
<executiondata>
<file file="${report.dir.file}"/>
</executiondata>
<structure name="JaCoCo Reports">
<classfiles>
<fileset dir="${dist.dir}/applications/lib/manager.jar">
<include name="**/*.class"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
</sourcefiles>
</structure>
<html destdir="${report.dir}/coverage"/>
</jacoco:report>
</target>
This is the empty report I get.
Regards,
Sat
I used the latest version 0.8.7 and it worked.

Creating Ant classpath out of project names

In an ant build script I have a list of projects we are depending on. I need to create a classpath for compilation.
I have:
included.projects=ProjectA, ProjectB
and I need:
included.project.classpath=../ProjectA/bin, ../ProjectB/bin
current code:
<echo message="${included.projects}" />
<pathconvert property="included.projects.classpath" dirsep="," >
<map from="" to="../"/>
<path location="${included.projects}"/>
</pathconvert>
<echo message="${included.projects.classpath}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath>
<pathelement path="${classpath}" />
<dirset includes="${included.projects.classpath}" />
</classpath>
</javac>
I've tried it with explicit declaration too, but didn't work:
<path id="modules.classpath">
<fileset dir="../ModuleA/bin" />
<fileset dir="../ModuleB/bin"/>
</path>
<path id="libraries.classpath">
<fileset dir="lib" includes="*.jar"/>
</path>
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
<classpath refid="modules.classpath" />
</javac>
I'm curious, what is the problem with explicit declaration code, and is it possible to solve with the comma-separated-string to classpath solution.
I think it would be simpler to explicity declare the classpath at the top of your build as follows:
<path id="compile.path">
<fileset dir="../ProjectA/bin" includes="*.jar"/>
<fileset dir="../ProjectB/bin" includes="*.jar"/>
</path>
Used as follows:
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath>
<path refid="compile.path"/>
<pathelement path="${classpath}" />
</classpath>
</javac>
Note:
I read your question again and just realised that you're not using jar files built by the other projects, are you? .... Not a great idea....

How to run dbunit task in ant?

It doesn't work in ant,
I'd like to run some classes that extend DatabaseTestCase
<path id="libs.dir">
<fileset dir="lib" includes="**/*.jar" />
</path>
<taskdef name="dbunit"
classname="org.dbunit.ant.DbUnitTask"/>
<!-- run all tests in the source tree -->
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
It says the following:
Buildfile: D:\kariakin\jdbc_task\build.xml
BUILD FAILED
D:\kariakin\jdbc_task\build.xml:15: taskdef class org.dbunit.ant.DbUnitTask cannot be found
using the classloader AntClassLoader[]
I think the problem is your taskdef, it's missing the path containing the dbunit jar:
<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="libs.dir"/>

Junit classpath in ant: problems with dirset

I'm getting troubles trying to use
<dirsets>
in my junit ant.
This is the snippet of the classpath.
<target name="myTests" >
<junit haltonerror="true" haltonfailure="true" fork="true">
<classpath>
<dirset dir="/my/absolute/root/path/where/I/keep/compiled/classes">
<include name="com/mycompany/mytests"/>
</dirset>
<pathelement location="my/path/to/jars/myjar1.jar" />
<pathelement location="my/path/to/jars/myjar2.jar" />
<!-- and so on -->
</classpath>
<test name="com.mycompany.mytests.MyFirstTest"
outfile="${dir.report.test}/report_MyFirstTest">
<formatter type="xml" />
</test>
</junit>
</target>
when I launch the test, after having successfully compiled all the code, ant complains:
java.lang.ClassNotFoundException: com.mycompany.mytests.MyFirstTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
I tried with absolute, relative paths and it never works. My classpath consists on many jars specified with many and that that is never recognized.
Where is my fault?
thanks
I former times when I used ant I used the nested <classpath> element and specified the classpath with the path-like structure - like this:
<path id="project.test.classpath">
<pathelement location="/my/absolute/root/path/where/I/keep/compiled/classes" />
<fileset dir="/my/path/to/jars">
<include name="**/*.jar" />
</fileset>
</path>
<target name="myTests">
<junit haltonerror="true" haltonfailure="true" fork="true">
<classpath refid="project.test.classpath" />
<test name="com.mycompany.mytests.MyFirstTest" outfile="${dir.report.test}/report_MyFirstTest">
<formatter type="xml" />
</test>
</junit>
</target>
Maybe that fit's also for you.

Running jUnit with ANT - How to execute all tests without using #Suite?

I'd like to execute all tests in a /test directory without using annotations such as
#Suite.SuiteClasses( ....)
In the past i had a single class, which was calling many other classes to test them all. This approach is no longer acceptable.
I have a /test directory, underneath which i have a number of packages, each containing several tests.
In my current ANT script, i have:
<target name="compileTest" depends="compile" description="compile jUnit">
<javac srcdir="${test}" destdir="${bin}" includeantruntime="true" />
</target>
followed by
<target name="test" depends="compileTest">
<junit printsummary="yes" fork="no" haltonfailure="no">
<classpath location="${bin}" />
<formatter type="plain" />
</junit>
</target>
In the past, i had
<test name="MyCollectionOfTests" />
I'd rather not do this anymore.
What am i missing? Please advise.
You can use a nested batchtest. For instance:
<junit printsummary="on"
fork="on"
dir="${test.build}"
haltonfailure="false"
failureproperty="tests.failed"
showoutput="true">
<classpath>
<path refid="tests.classpath"/>
</classpath>
<batchtest todir="${test.report}">
<fileset dir="${test.gen}">
<include name="**/Test*.java"/>
</fileset>
<fileset dir="${test.src}">
<include name="**/Test*.java"/>
<exclude name="gen/**/*"/>
</fileset>
</batchtest>
</junit>
In its simplest form, you can simply add a nested:
<batchtest todir="report">
<fileset dir="test"/>
</batchtest>
to your junit call.

Resources