I am getting a java.lang.ClassNotFoundException when I run my target TranslatorWorkflow that is supposed to execute a JUnit test. I am running a build.xml file with the targets: build TranslatorWorkflow. It compiles but fails on the JUnit test immediately.
My TranslatorWorkflow.class file is in {basedir}/bin/testScripts/. My classpath and target are:
classpath:
<path id="classpath">
<fileset dir="${basedir}/lib/binaries" includes="*.jar" />
<pathelement location="${basedir}/bin/testScripts/" />
</path>
TranslatorWorkflow target in my build.xml file:
<target name="TranslatorWorkflow">
<mkdir dir="${junit.output.dir}" />
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml" />
<test name="testScripts.TranslatorWorkflow" todir="${junit.output.dir}" />
<classpath refid="classpath" />
</junit>
</target>
I attempted to emulate this answer to a similar question by adding the pathelement line shown in my classpath section above, but received the same exception. I've looked at this question as well as it seems like the same deal. I'd imagine there is something super obvious that I'm missing but alas I don't seem to be getting it.
The classpath should reference ${basedir}/bin not ${basedir}/bin/testScripts (i.e. it should reference the root of classes directory, not the package in which the class exists):
<path id="classpath">
<fileset dir="${basedir}/lib/binaries" includes="*.jar" />
<pathelement location="${basedir}/bin/" />
</path>
Dumpcats, try this...
<path id="base.path">
<pathelement path="${sun.boot.class.path}"/>
<pathelement path="${sun.boot.library.path}"/>
<fileset dir="${basedir}">
<include name="**.jar"/>
</fileset>
</path>
And then in the target element ...
<target name="runTest">
<mkdir dir="test_reports"/>
<junit
fork="true"
forkmode="once"
maxmemory="256m">
<formatter type="plain" usefile="false"/>
<formatter type="xml"/>
<classpath refid="base.path"/>
<batchtest
haltonerror="true" haltonfailure="true"
todir="test_reports">
<fileset dir="${test.build}">
<include name="**/*Test*.class"/>
<include name="**/Test*.class"/>
<include name="**/*Test.classp"/>
<!-- Exclusions -->
<exclude name="**/util/*.class"/>
</fileset>
</batchtest>
</junit>
</target>
At least is that is how i manage classpath reference and everything works like a charm.
Related
I am trying to write a ant build which calls a Java class
<target name="validate" depends="forge-jar">
<taskdef name="spec" classname="SpecBuild" classpathref="java.classpath" />
<spec a="${forge}" mail="${mail}" dd="${dd}" wdd="${wdd}"/>
</target>
<target name="forge-jar" >
<path id="project.class.path">
<pathelement location="classes/" />
<pathelement path="classes/ant/tasks/SpecBuild.class" />
</path>
<path id="java.classpath">
<path refid="project.class.path" />
</path>
<javac includeantruntime="false" srcdir="src" destdir="classes" classpathref="java.classpath">
<filename name="**/SpecBuild.java" />
</javac>
</target>
In the SpecBuild.java class if I use slf4j logger I am getting this warning :-
CLASSPATH element /Users/classes/ant/tasks/SpecBuild.class is not a JAR.
[taskdef] CLASSPATH element /Users/classes/ant/tasks/SpecBuild.class is not a JAR.
Can anyone please help me fix this
You can specify the 'classes' directory in the class path as below. Remove the 'SpecBuild.class' file from pathElement location. You can either use 'includes' property and mention the file name in it or just include the 'classes' directory as below (exclude the JUni test cases package, if you have).
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
<dirset dir="${build.dir}">
<include name="apps/**/classes"/>
<exclude name="apps/**/*Test*"/>`enter code here`
</dirset>
</classpath>
Good luck.
I'm trying to create a Proof of concept for the actual application, The task is to run ant-junit tests without adding stuff to the global environment. I've achieved it so far in ant, but, I'm stuck with the following exception. ClassTest.java is the java class that has sample unit testcase. (Junit 3.0 style). I'm not sure why ant-junit does not find the class inspite of mentioning the paths in the batchrun task.
project structure
I get the following exception when running ant-junit task.
Testsuite: ClassTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Caused an ERROR
ClassTest
java.lang.ClassNotFoundException: ClassTest
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
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)
My ant build.xml file is
<project name="Java project build" default="build">
<property name="project.local.directory" value="C:\Users\usrpao\workspace\Ant" />
<property name="src.path" value="${project.local.directory}/src" />
<property name="lib.path" value="${project.local.directory}/lib" />
<property name="dest.path" value="${project.local.directory}/target" />
<property name="junit.output.dir" value="${project.local.directory}/junit" />
<path id="classpath.test">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
<!--<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath refid="classpath.test" />
</taskdef> -->
<path id="MyProject.classpath">
<pathelement location="lib/ant-junit.jar" />
<pathelement location="lib/junit.jar" />
<pathelement location="bin/*.*"/>
</path>
<path id="lib.classpath">
<pathelement location="lib/ant-junit.jar" />
<pathelement location="lib/junit.jar" />
</path>
<target name="build" depends="clean">
<javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath">
</javac>
<antcall target="test">
</antcall>
</target>
<target name="clean">
</target>
<target name="test">
<pathconvert property="testoutput" refid="classpath.test" />
<echo>Path = ${testoutput}</echo>
<mkdir dir="${junit.output.dir}" />
<junit haltonerror="false" showoutput="true">
<classpath refid="MyProject.classpath">
</classpath>
<batchtest todir="${junit.output.dir}">
<formatter type="plain" />
<fileset dir="${src.path}/com/ant/test">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
Your classpath is referencing the bin directory while the classes are actually under the target directory. You should change the MyProject.classpath reference to include ${project.local.directory}/target:
<path id="MyProject.classpath">
<pathelement location="lib/ant-junit.jar" />
<pathelement location="lib/junit.jar" />
<dirset dir="${project.local.directory}">
<include name="target"/>
</dirset>
</path>
In addition, you should change the fileset element inside the junit task by removing the folders corresponding to the package name of the test class, otherwise you will still get a ClassNotFoundException:
<junit haltonerror="false" showoutput="true">
<classpath refid="MyProject.classpath">
</classpath>
<batchtest todir="${junit.output.dir}">
<formatter type="plain" />
<fileset dir="${src.path}"> <!-- remove com/ant/test corresponding to package name -->
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
From your folder structure it is evident that the class ClassTest.class is in target folder. so in class-path you should include following:
<pathelement path="target"/>
Hope this helps.
I can run my tests via Ant in IDE.
But, While i try to run it from Command Line - it fails, because cant find the Excel from Resources
I added:
`<copy todir="test/Resources/Data">
<fileset dir="${Resources}/Data">
<exclude name="**/*.java"/>
</fileset>
</copy>`
the file copied but still fail.
Looks like code doesnt look in correct place..
Any idea?
full build.xml:
<project name="TestNGTest" default="test" basedir=".">
<!-- Define <testng> task -->
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="lib/testng-6.8.5.jar"/>
</classpath>
</taskdef>
<property name="testdir" location="test" />
<property name="srcdir" location="src" />
<property name="libdir" location="lib" />
<property name="full-compile" value="true" />
<property name="Resources" location="Resources"/>
<copy todir="test/Resources/Data">
<fileset dir="${Resources}/Data">
<exclude name="**/*.java"/>
</fileset>
</copy>
<path id="classpath.base"/>
<path id="classpath.test">
<fileset dir="${libdir}">
<include name="**/*.jar" />
</fileset>
<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">
<testng outputdir=".test-output" classpathref="classpath.test"
useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter">
<xmlfileset dir="." includes="testng.xml"/>
</testng>
</target>
</project>'
i found the problem.
in code i use the System.getProperty("user.dir") and when running from ant it finds my "user" folder.
So i can manually put there the excel ant it works.
Now, i need to find the way to change the System.getProperty("user.dir") to something that points to projects root
I have a directory with a bunch of JUnit tests in my project. So far I have used separate target for each unit test. For example:
<target name="MyTest">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="tests.MyTest" todir="${junit.output.dir}"/>
<classpath refid="MyProject.classpath"/>
</junit>
</target>
This method requires me to change build file every time I add a Unit test.
I want to able able to to run all unit tests in the project with a single Ant builder target. Is it possible to do?
Yep it is, you need to look at the fileset tag, e.g:
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${MyProject.classpath}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
The important part is the use of fileset and a glob/wildcard pattern to match the names of the tests. Full docs on the junit task with examples here:
http://ant.apache.org/manual/Tasks/junit.html
Yep! We do it using an ant command batchtest. Looks like this:
<batchtest todir="${junit.report.dir}">
<fileset dir="${basedir}\test\unit">
<include name="**/*Test.java" />
</fileset>
</batchtest>
Google it, it should sort you out
I am using ant script and running a junit, the problem I am facing is that I am unable to get the output on console, rather I get the output in a log file.
how can I achieve that..
I am using the following script.
<target name="validate_mapping" description="Testing the Hibernate ">
<path id="validator.classpath">
<fileset dir="${basedir}\lib">
<include name="Junit-Temp-Test.jar" />
</fileset>
</path>
<junit printsummary="yes">
<formatter type="plain"/>
<test name="com.ofss.fc.junit.test.SampleTest" />
<classpath>
<path refid="validator.classpath" />
<path refid="lib.ext.classpath" />
</classpath>
</junit>
</target>
I think you need to specify "usefile" attribute <formatter usefile="false" type="plain"/>