Junit classpath in ant: problems with dirset - ant

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.

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.

Can't run Groovy test cases from 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.

Ant jUnit WebDriver - NoClassDefFoundError

I am struggling with Ant these days, trying to make it driver my WebDriver tests. So far I got to the following build.xml ( blatantly copied from somewhere )
<property name="src" value="./src" />
<property name="lib" value="d:/apache-ant-1.8.4/lib/" />
<property name="bin" value="./bin/" />
<property name="report" value="./report" />
<path id="test.classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
</target>
<target name="compile" depends="init">
<javac source="1.6" srcdir="${src}" fork="true" destdir="${bin}" >
<classpath>
<pathelement path="${bin}">
</pathelement>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="exec" depends="compile">
<delete dir="${report}" />
<mkdir dir="${report}" />
<mkdir dir="${report}/xml" />
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
<test name="com.yourcompany.selenium.ccloop.tb6NoInterested" haltonfailure="no" todir="${report}/xml" outfile="TEST-result">
<formatter type="xml" />
</test>
</junit>
<junitreport todir="${report}">
<fileset dir="${report}/xml">
<include name="TEST*.xml" />
</fileset>
<report format="frames" todir="${report}/html" />
</junitreport>
</target>
Now, when I run ant everything gets build fine, but the test does not run and I am getting the NoClassDefFoundError.
org/apache/http/HttpHost
java.lang.NoClassDefFoundError: org/apache/http/HttpHost at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:144)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:86)
at com.yourcompany.selenium.ccloop.tb6NoInterested.setUp(Unknown
Source) Caused by: java.lang.ClassNotFoundException:
org.apache.http.HttpHost 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
java.lang.ClassLoader.loadClass(ClassLoader.java:247) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247) N/A
java.lang.NullPointerException at
com.yourcompany.selenium.ccloop.tb6NoInterested.tearDown(Unknown
Source)
Package name is com.yourcompany.selenium.ccloop
Test name is tb6NoInterested
I have all the jars in ant lib folder ( the hamcrest, junit, selenium ones )
What am I doing wrong?
It seems that httpcore from apache is not in the classpath.
org/apache/http/HttpHost is a class in that library.
I usually use findjar to find which jars contain classes, when I get a surprising NoClassDefFoundError.

Empty Junit reports from ant

I am trying to use ant to run junit tests and generate reports.
I am able to successfully run the tests but the report files are empty.
What am I doing wrong ?
This is my build.xml :
<project name="JunitTest" default="test" basedir=".">
<property name="testdir" location="." />
<property name="srcdir" location="." />
<property name="full-compile" value="true" />
<property name="test.reports" value="./reports" />
<path id="classpath.base"/>
<path id="classpath.test">
<pathelement location="${testdir}" />
<pathelement location="${srcdir}" />
<path refid="classpath.base" />
</path>
<target name="clean" >
<delete verbose="${full-compile}">
<fileset dir="${testdir}" includes="**/*.class" />
</delete> `
</target>
<target name="compile" depends="clean">
<javac srcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}" >
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="com.tests.nav1" />
</junit>
<junitreport todir="${test.reports}">
<fileset dir="${test.reports}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.reports}" />
</junitreport>
</target>
</project>
and this is the output on the console :
[junit] Using CLASSPATH C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src;C:\jars\junit.jar;C:\ant\lib\ant-launcher.jar;C:\ant\lib\ant.jar;C:\ant\lib\ant-junit.jar;C:\ant\lib\ant-junit4.jar
[junit] Testsuite: com.tests.nav1
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 48.187 sec
[junit] ------------- Standard Output ---------------
[junit] testnav2
[junit] ------------- ---------------- ---------------
[junitreport] Using class org.apache.tools.ant.taskdefs.optional.TraXLiaison
[junitreport] Processing C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\reports\TESTS-TestSuites.xml to C:\Users\pmahajan\AppData\Local\Temp\null236099757
[junitreport] Loading stylesheet jar:file:/C:/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 330ms
[junitreport] Deleting: C:\Users\pmahajan\AppData\Local\Temp\null236099757
BUILD SUCCESSFUL
Total time: 49 seconds
If you look at the ant snippet, there are a few issues:
You have set usefile=false, which means no output file is created
You have set formatter type=brief, which will print detailed information only for failed tests
You need to also specify the todir - the folder where the report has to go in the <test> tag - the default is current folder. This should match the folder you are using in <junitreport> task.
You can try with the following updated <junit> section...
<junit>
<classpath refid="classpath.test" />
<formatter type="xml"/>
<test name="com.tests.nav1" todir="${test.reports}"/>
</junit>
1 <target name="test" depends="compile">
2 <junit>
3 <classpath refid="classpath.test" />
4 <formatter type="brief" usefile="false" />
5 <test name="com.tests.nav1" />
6 </junit>
7 <junitreport todir="${test.reports}">
8 <fileset dir="${test.reports}">
9 <include name="TEST-*.xml" />
10 </fileset>
11 <report todir="${test.reports}" />
12 </junitreport>
13 </target>
The above segment of your coded needs following changes.
You have to specify the to directory option in the 5th line( for example todir = ${data.reports} )
In the 8th line the directory specified must me data.reports.
The 11th line must contain the option format with the value frames (format="frames").
The ant JUnit Task doc gives this example that might help you (as it apparently does exactly what you're trying to achieve):
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${java.class.path}"/>
</classpath>
<formatter type="plain"/>
<test name="my.test.TestCase" haltonfailure="no" outfile="result">
<formatter type="xml"/>
</test>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
Runs my.test.TestCase in the same VM, ignoring the given CLASSPATH; only a warning is printed if this test fails. In addition to the plain text test results, for this test a XML result will be output to result.xml. Then, for each matching file in the directory defined for ${src.tests} a test is run in a separate VM. If a test fails, the build process is aborted. Results are collected in files named TEST-name.txt and written to ${reports.tests}.
It is specified in the doc that printsummary can take values on and off, but they're using yes in the example which is on the same page, so I guess it's accepted too.
Please try formatter with "xml"
<formatter type="${junit.format}"/>
where junit.format is property with appropriate value.

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