ClassNotFoundException - when running ant-junit - java

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.

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" />

JUnit Ant ClassNotFoundException

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.

dataDriven excel - ant via commandLine( testng ant webdriver)

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

Cobertura + Junit

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>

Ant + Junit Issues

I am trying to get Junit work with Ant. I have come across questions on the topic. I guess there is some small error somewhere but I can't figure it out. Here is my Build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="IvleFileSync" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<!-- Variables used for JUnit testing -->
<property name="test.dir" location="test" />
<property name="test.report.dir" location="test-reports" />
<path id="junit-classpath">
<fileset dir="${test.dir}">
<include name = "*" />
</fileset>
</path>
<path id="files-classpath">
<fileset dir="/usr/lib" >
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
<mkdir dir="${test.report.dir}" />
</target>
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}">
<classpath>
<path refid="files-classpath" />
</classpath>
</javac>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/IvleFileSync-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="compile-test" depends="compile" description="compile the tests " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${test.dir}" destdir="${build}">
<classpath>
<path refid="files-classpath" />
</classpath>
</javac>
</target>
<target name="test" depends="compile-test" description="Execute Unit Tests" >
<junit printsummary="yes" fork="yes" haltonfailure="yes" showoutput="false">
<classpath >
<path refid="files-classpath" />
<path refid= "junit-classpath" />
</classpath>
<batchtest fork="yes" todir="${test.report.dir}/">
<formatter type="xml"/>
<fileset dir="${test.dir}">
<include name="*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${test.report.dir}" />
<delete dir="${dist}"/>
</target>
</project>
And I have test files in /test directory as well as i have put the jars in ANT_HOME/lib
That does not work and I get this error when I dig up the test-results/....xml
<error message="NewEmptyJUnitTest" type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: NewEmptyJUnitTest
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)
Thanks for helping me out...
The classpath for the junit task does not include the output of the compile-test target. Use something like the following for the JUnit classpath:
<classpath>
<pathelement path="${build}"/>
<path refid="files-classpath" />
<path refid="junit-classpath" />
</classpath>
You forgot to add your own classes ("build") to the "test" target.

Categories