java.uti.zip.ZipException error while calling a ant target - java

While calling a ANT target, I am getting the below error. I tried my level best solve the issue but not.
Unable to obtain resource from C:\uiunit\log4j.properties: java.util.zip.ZipException: error in opening zip file
[junit] Unable to obtain resource from C:\uiunit\log4j.properties:
[junit] java.util.zip.ZipException: error in opening zip file
[junit] at java.util.zip.ZipFile.open(Native Method)
[junit] at java.util.zip.ZipFile.<init>(ZipFile.java:214)
[junit] at java.util.zip.ZipFile.<init>(ZipFile.java:144)
[junit] at java.util.jar.JarFile.<init>(JarFile.java:152)
[junit] at java.util.jar.JarFile.<init>(JarFile.java:116)
[junit] at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLoader.java:1006)
[junit] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.findNextResource(AntClassLoader.java:149)
[junit] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.<init>(AntClassLoader.java:110)
[junit] at org.apache.tools.ant.AntClassLoader.findResources(AntClassLoader.java:953)
[junit] at org.apache.tools.ant.AntClassLoader.getNamedResources(AntClassLoader.java:922)
[junit] at org.apache.tools.ant.loader.AntClassLoader5.getResources(AntClassLoader5.java:58)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.checkForkedPath(JUnitTask.java:1181)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1061)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:855)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1903)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:804)
[junit] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
[junit] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[junit] at java.lang.reflect.Method.invoke(Method.java:601)
[junit] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[junit] at org.apache.tools.ant.Task.perform(Task.java:348)
[junit] at org.apache.tools.ant.Target.execute(Target.java:435)
[junit] at org.apache.tools.ant.Target.performTasks(Target.java:456)
[junit] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
[junit] at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
[junit] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[junit] at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
[junit] at org.apache.tools.ant.Main.runBuild(Main.java:851)
[junit] at org.apache.tools.ant.Main.startAnt(Main.java:235)
[junit] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
[junit] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
[junit] DEBUG com.cordys.cm.uiunit.config.IConfiguration - Using config file: "/C:/uiunit/UIUnit.properties" (-2009166949)
[junit] null
[junit] Running test.CapsInstallerTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.184 sec
[junit] Test test.CordysCapsInstallerTest FAILED
[junitreport] Processing D:\checkouts\GUIInstaller\TESTS-CapsInstallerTest-Suites.xml to D:\checkouts\GUIInstaller\guiunit.build.junit.report\junit-nof
[junitreport] Loading stylesheet jar:file:/D:/checkouts/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-noframes.xsl
[junitreport] Transform time: 499ms
Observation:- If I perform compile operation through eclipse then ANT target call is working wise. Bu If I perform compile operation through ANT then ANT target call is failing with the above error.
Buil.xml File content
<project name="GUITest" basedir="." default="installBOP">
<property environment="env" />
<property file="installer.properties"/>
<property file="uninstaller.properties"/>
<property file="c:/uiunit/uiunit.properties" />
<property name="src" location="src" />
<property name="src.test" location="src/test" />
<property name="fit" location="fit" />
<property name="bin" location="bin" />
<property name="lib" location="lib" />
<property name="guiunit.build.junit.report" location="guiunit.build.junit.report" />
<property name="report" location="report" />
<property name="report.run" location="report/run" />
<property name="REPORTNAME" location="CordysCapsInstallerTest" />
<path id="lib.classpath">
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="test.classpath">
<path refid="lib.classpath" />
<pathelement location="${bin}" />
<pathelement location="C:/uiunit" />
</path>
<condition property="OsName" value="Windows">
<os family="windows"/>
</condition>
<condition property="OsName" value="Linux">
<os name="Linux"/>
</condition>
<condition property="OsName" value="aix">
<os name="AIX"/>
</condition>
<condition property="OsName" value="SunOS">
<os name="SunOS"/>
</condition>
<condition property="isUnix">
<os family="unix"/>
</condition>
<condition property="onOracle">
<equals arg1="${env.DB_VENDOR_NAME}" arg2="Oracle 9i / 10g / 11g"/>
</condition>
<condition property="onWindows">
<os family="Windows"/>
</condition>
<condition property="onLinux">
<os name="Linux"/>
</condition>
<condition property="onAix">
<os name="AIX"/>
</condition>
<condition property="is64bit" value="-64bit" else="">
<and>
<os arch="amd64"/>
<not>
<equals arg1="${branch.name}" arg2="BOP4-CU"/>
</not>
<not>
<equals arg1="${branch.name}" arg2="BOP4-CU-Release"/>
</not>
</and>
</condition>
<target name="help">
<echo>ant upgrade: Upgrade the cordys</echo>
<echo>ant install: Install the cordys</echo>
<echo>ant uninstall: Uninstall the cordys</echo>
</target>
<target name="clean">
<delete dir="${bin}" />
<delete dir="${report}" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${bin}" />
<!--<javac includeantruntime="false" srcdir="${src}" destdir="${bin}">
<classpath refid="test.classpath" />
</javac>-->
<javac destdir="${bin}" debug="${debug}" nowarn="${debug}" deprecation="${deprecation}" optimize="${optimize}">
<classpath refid="test.classpath" />
<src path="${src}" />
</javac>
</target>
<target name="copy-installer" unless="dont.download">
<echo message="Fetching build number...." />
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${lib}/commons-net-2.2.jar" />
<ftp
action="get"
server="${ftp.server.name}"
userid="${ftp.user.name}"
remotedir="${ftp.remote.dir}/${branch.name}"
password="${ftp.user.password}"
passive="true">
<fileset dir="${basedir}">
<include name="build.number" />
<include name="build.properties"/>
</fileset>
</ftp>
<move file="build.number" tofile="buildnumber.properties"/>
<property file="${basedir}/build.properties"/>
<property file="${basedir}/buildnumber.properties"/>
<delete dir="${basedir}/installers" failonerror="false"/>
<condition property="buildfolder" value="" else="/build.${build.number}">
<or>
<equals arg1="${branch.name}" arg2="BOP4-CU"/>
<equals arg1="${branch.name}" arg2="BOP4-CU-Release"/>
</or>
</condition>
<echo message="Fetching installer.... from ${ftp.remote.dir}/${branch.name}${buildfolder}/external/${OsName}${is64bit}" />
<ftp
action="get"
server="${ftp.server.name}"
userid="${ftp.user.name}"
remotedir="${ftp.remote.dir}/${branch.name}${buildfolder}/external/${OsName}${is64bit}"
password="${ftp.user.password}"
passive="true">
<fileset dir="${basedir}/installers">
<include name="${name.dist.zip.file}"/>
</fileset>
</ftp>
<unzip src="${basedir}/installers/${name.dist.zip.file}" dest="${basedir}/installers"/>
<chmod dir="${basedir}/installers" perm="ugo+rx" includes="*.bin" osfamily="unix"/>
<delete file="${basedir}/installers/${name.dist.zip.file}"/>
</target>
<target name="installTilBaseline" depends="compile,copy-installer">
<echo message="${log4j.rootCategory}"/>
<mkdir dir="${report.run}" />
<java fork="true" classname="test.GUITest" failonerror="true">
<arg line="install"/>
<!--jvmarg line="-Dlog4j.configuration=${basedir}/lib/log4j.properties"/-->
<classpath>
<path refid="test.classpath"/>
<path location = "$(bin)" />
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
<pathelement location="${env.Path}"/>
</classpath>
</java>
<echo message="${env.classpath}"/>
</target>
<target name="uninstallBOP" depends="compile">
<mkdir dir="${report.run}" />
<java fork="true" classname="test.GUITest" failonerror="true">
<arg line="uninstall"/>
<classpath>
<path refid="test.classpath"/>
<path location = "$(bin)" />
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
<pathelement location="${env.Path}"/>
</classpath>
</java>
<echo>Please see the run report page for more details</echo>
</target>
<target name="upgradeTilBaseline" depends="compile,copy-installer">
<mkdir dir="${report.run}" />
<java fork="true" classname="test.GUITest" failonerror="true">
<arg line="upgrade"/>
<classpath>
<path refid="test.classpath"/>
<path location = "$(bin)" />
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
<pathelement location="${env.Path}"/>
</classpath>
</java>
<echo>Please see the run report page for more details</echo>
</target>
<target name="launch-rc-server" if="${selenium.server.ant}" >
<mkdir dir="${report.run}"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${lib}/ant-contrib-1.0b1.jar" />
<taskdef name="stringutil" classname="ise.antelope.tasks.StringUtilTask" classpath="${lib}/antelopetasks-3.2.10.jar" />
<stringutil string="${seleniumserver}" property="indexofcom">
<indexof string=":" />
</stringutil>
<stringutil string="${seleniumserver}" property="temp">
<substring beginindex="${indexofcom}" />
</stringutil>
<stringutil string="${temp}" property="hub.port">
<replace regex=":" replacement="" />
</stringutil>
<if>
<equals arg1="${browsertype}" arg2="safariproxy" />
<then>
<property name="selenium.server" value="selenium-server-standalone-2.0b3.jar"/>
</then>
<else>
<property name="selenium.server" value="selenium-server-standalone-2.32.0.jar"/>
</else>
</if>
<if>
<istrue value="${browser.session.reuse}"/>
<then>
<property name="temp.selenium.args" value="-port ${hub.port} -browserSessionReuse" />
</then>
<else>
<property name="temp.selenium.args" value="-port ${hub.port}" />
</else>
</if>
<if>
<isset property="selenium.args.custom"/>
<then>
<property name="selenium.args" value="${temp.selenium.args} ${selenium.args.custom}" />
</then>
<else>
<property name="selenium.args" value="${temp.selenium.args}" />
</else>
</if>
<echo>/K start java -jar lib/${selenium.server} ${selenium.args}</echo>
<exec dir="." executable="cmd" spawn="false" output="${report.run}/startsel-output.txt" logerror="true">
<arg line="/K start java -jar lib/${selenium.server} ${selenium.args}"/>
</exec>
<sleep seconds="10"/>
<antcall target="waitForSeleniumServer" />
</target>
<target name="waitForSeleniumServer" description="wait till selenium server starts" if="${selenium.server.ant}">
<echo taskname="waitfor" message="Wait for proxy server launch" />
<waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
<http url="http://${seleniumserver}/selenium-server/driver/?cmd=testComplete"/>
</waitfor>
</target>
<target name="shutdown-rc-server" description="this will shutdown RC" if="${selenium.server.ant}">
<echo>seleniumserver is ${seleniumserver}</echo>
<java classname="com.selenium.remotecontrol.ShutDownRC" fork="true" failonerror="true">
<jvmarg value="-Durl=http://${seleniumserver}" />
<classpath>
<fileset dir="${basedir}/lib">
<include name="commons-httpclient-3.0.1.jar" />
<include name="commons-logging-*.jar" />
<include name="commons-codec-1.5.jar" />
<include name="shutdownrc.jar" />
</fileset>
</classpath>
</java>
<sleep seconds="3"/>
</target>
<target name="installcaps" depends="launch-rc-server">
<echo>${test.classpath}</echo>
<mkdir dir="${guiunit.build.junit.report}" />
<!-- do necessary setup-->
<junit fork="yes" failureproperty="junit.failed" dir="${basedir}"
filtertrace="on"
haltonfailure="no" printsummary="on"
showoutput="yes">
<classpath>
<path refid="test.classpath"/>
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
<pathelement location="${env.Path}"/>
<pathelement location="${bin}"/>
</classpath>
<formatter type="xml"/>
<test name="test.CordysCapsInstallerTest" todir="${guiunit.build.junit.report}"/>
</junit>
<junitreport tofile="TESTS-CordysCapsInstallerTest-Suites.xml">
<fileset dir="${guiunit.build.junit.report}">
<include name="**/TEST-*.xml"/>
</fileset>
<report format="noframes" todir="${guiunit.build.junit.report}"/>
</junitreport>
<antcall target="shutdown-rc-server" />
</target>
<target name="installBOP" depends="installTilBaseline,installcaps">
<echo>Installation of BOP is completed</echo>
</target>
<target name="upgradeBOP" depends="upgradeTilBaseline,installcaps">
<echo>Upgrade of BOP is completed</echo>
</target>
<target name="buildjar">
</target>
when I am calling $> ant installcaps target previously I used to get the above error, after that by following Guillaume Darmont comment I removed the C:\uiunit\log4j.properties value from class path, then I landed in to the below error
D:\checkouts\GUIInstaller>ant installcaps
Buildfile: D:\checkouts\GUIInstaller\build.xml
launch-rc-server:
[mkdir] Created dir: D:\checkouts\GUIInstaller\report\run
[echo] /K start java -jar lib/selenium-server-standalone-2.32.0.jar -port 4444
waitForSeleniumServer:
[waitfor] Wait for proxy server launch
installcaps:
[echo] ${test.classpath}
[junit] DEBUG com.cordys.cm.uiunit.config.IConfiguration - Using config file: "/C:/uiunit/UIUnit.properties" (-2009166949)
[junit] Running test.CordysCapsInstallerTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.191 sec
[junit] Test test.CordysCapsInstallerTest FAILED
[junitreport] Processing D:\checkouts\GUIInstaller\TESTS-CordysCapsInstallerTest-Suites.xml to D:\checkouts\GUIInstaller\guiunit.build.junit.report\junit-noframes.html
[junitreport] Loading stylesheet jar:file:/D:/checkouts/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-noframes.xsl
[junitreport] Transform time: 524ms
shutdown-rc-server:
[echo] seleniumserver is cin400695:4444
[java] RC running in .........:4444 is shutdown
BUILD SUCCESSFUL
Total time: 17 seconds
And in the log files I seen the below error.
testcase classname="test.CordysCapsInstallerTest" name="initializationError0" time="0.007">
<error type="java.lang.NullPointerException">java.lang.NullPointerException
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at com.cordys.cm.uiunit.junit4.internal.runners.UIUnitClassMethodsRunner.getTestMethods(UIUnitClassMethodsRunner.java:168)
at com.cordys.cm.uiunit.junit4.internal.runners.UIUnitClassMethodsRunner.<init>(UIUnitClassMethodsRunner.java:45)
at com.cordys.cm.uiunit.junit4.runners.UIUnitRunner.<init>(UIUnitRunner.java:15)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
</testcase>

This kind of error is often caused by files other than JARs in the classpath of your <junit> Ant task.
An Ant classpath can only contains :
JARs files
ZIP files
directories
So you have to check your classpath configuration, it seems that it contains C:\uiunit\log4j.properties, probably instead of C:\uiunit\.
BTW, you should have post your build file, bug would have been easier to track.

Related

Junit test fails but build is successful (Ant)

Hi i'm quite new to using ant, my instructor wanted us to use it to do some tasks like build the class, run tests..etc
and i am really confused since i cant make the junit work. i have read several other posts and sources but it makes me even more confused.
the error i am getting is:
Testsuite: SolutionTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
Caused an ERROR
SolutionTest
java.lang.ClassNotFoundException: SolutionTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
this is my build file:
<project name="Solution" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="doc.dir" value="${build.dir}/documentation" />
<property name="test.dir" value="${build.dir}/test" />
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
</jar>
</target>
<target name="clean" depends="jar">
<delete dir="${classes.dir}"/>
</target>
<target name="doc" depends="clean">
<mkdir dir="${doc.dir}" />
<javadoc destdir="${build.dir}/documentation" sourcefiles="${src.dir}/*.java" />
</target>
<target name="junit" depends="doc">
<mkdir dir="${test.dir}" />
<junit printsummary="yes" haltonfailure="no">
<classpath location="." />
<test name="SolutionTest" haltonfailure="no" todir="${test.dir}" outfile="result">
<formatter type="plain" />
<formatter type="xml" />
</test>
</junit>
</target>
<target name="main" depends="clean, junit"/>
</project>
You should use <junit haltOnFailure="yes" haltOnError="yes" ...> if you want build to fail.
Alternatively you can use failureProperty and errorProperty
<junit failureProperty="test.failure" errorProperty="test.error">
<!-- your code here -->
</junit>
And verify if those properties was set
<target name="main" depends="clean, junit, verifyNoError, verifyNoFailure"/>
<target name="verifyNoError" if="test.error">
<fail message="JUnit test or tests errors."/>
</target>
<target name="verifyNoFailure" if="test.failure">
<echo message="I'm here. Now what?"/>
<fail message="JUnit test or tests failed."/>
</target>

Ant classNotFound exception with Junit Java

I am getting a ClassNotFound exception when I try to run my unit test.
Here is the .xml script for running the test.
<?xml version="1.0"?>
<project name="Embedded CST316 Project Test Build" default="targets" basedir=".">
<!-- set global properties for this build -->
<property name="src" location="airAutomation/src" />
<!-- <property name="test" location="airAutomation/test" /> -->
<property name="test" location="airAutomation/testAirUI/airUI/pkg" />
<property name="build" location="airAutomation/classes" />
<property name="test.dir" location="airAutomation/classes/tests" />
<property name="test.report.dir" location="airAutomation/testreport" />
<path id="junit.class.path">
<pathelement location="${build}" />
<pathelement location="airAutomation/lib/xbjlib-1.0.1.jar" />
<pathelement location="airAutomation/lib/junit-4.12.jar" />
<pathelement location="airAutomation/lib/hamcrest-core-1.3.jar" />
</path>
<target name="targets">
<!-- Display available targets -->
<echo message="targets are clean, prepare, compile, junit"/>
</target>
<target name="clean">
<!-- Delete the ${build} directory tree -->
<delete dir="${build}" />
<delete dir="${test.report.dir}" />
<!-- <delete dir="${test}" /> -->
</target>
<target name="prepare">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}" />
<!-- <mkdir dir="${test}" /> -->
<mkdir dir="${test.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<target name="compile" depends="clean, prepare"
description="Compile Unit Tests">
<!-- Compile all classes -->
<javac srcdir="${src}"
includeantruntime="false"
destdir="${build}" >
<classpath refid="junit.class.path" />
</javac>
<javac srcdir="${test}"
includeantruntime="false"
destdir="${test.dir}" >
<classpath refid="junit.class.path" />
</javac>
</target>
<target name="junit" depends="compile"
description="Execute Unit Tests">
<!-- Run Junit tests -->
<junit printsummary="yes" fork="true" haltonfailure="yes">
<classpath refid="junit.class.path" />
<classpath>
<pathelement location="${test.dir}" />
</classpath>
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="airAutomation/testAirUI/airUI/pkg/">
<include name="**Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
</project>
Here is the output from the end of the xml.
<error message="roomTest" type="java.lang.ClassNotFoundException">
java.lang.ClassNotFoundException: roomTest
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:171)
</error>
The test is in the folder "airAutomation/testAirUI/airUI/pkg/"
Would I need to specify this folder somewhere else (I created some folders to hold the output of the tests)?

ClassNotFoundException when running ant junit

I'm running into this ClassNotFoundException issue when running Junit task below, but this is non eclipse based project running an ant build from command prompt, the build file is pasted below. The exception is below. I have done similar to that of setup here it does not work though, any pointers will be appreciated. Thanks.
Class org.apache.tools.ant.util.StringUtils loaded from parent loader (parentFirst)
[junit] Testsuite: SampleTest
Class org.apache.tools.ant.util.FileUtils loaded from parent loader (parentFirst)
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] SampleTest
[junit] java.lang.ClassNotFoundException: SampleTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:219)
[junit]
Build.xml
<project name="Java project build" default="test">
<property name="project.local.directory" value="." />
<property name="src.path" value="${project.local.directory}/SampleUnitTest/com" />
<property name="lib.path" value="${project.local.directory}/APP-INF/lib" />
<property name="dest.path" value="${project.local.directory}/SampleUnitTest/target" />
<property name="junit.output.dir" value="${project.local.directory}/junit" />
<path id="MyProject.classpath">
<pathelement location="${lib.path}/ant-junit.jar" />
<pathelement location="${lib.path}/junit.jar" />
<pathelement location="${lib.path}/SampleUnitTest.jar" />
<dirset dir="${project.local.directory}/SampleUnitTest">
<include name="target" />
</dirset>
</path>
<path id="lib.classpath">
<pathelement location="${lib.path}/ant-junit.jar" />
<pathelement location="${lib.path}/junit.jar" />
<fileset dir="${lib.path}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="test" description="Tests the java files" depends="build">
<mkdir dir="${junit.output.dir}" />
<junit>
<classpath refid="MyProject.classpath">
</classpath>
<batchtest todir="${junit.output.dir}">
<formatter type="plain" usefile="false" />
<fileset dir="${src.path}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="build" description="Tests the java files" depends="clean">
<mkdir dir="${dest.path}" />
<javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath">
</javac>
<jar destfile="${lib.path}/SampleUnitTest.jar" basedir="${dest.path}"/>
</target>
<target name="clean" description="Tests the java files">
<delete dir="${dest.path}">
</delete>
</target>
</project>
Update
package com;
import com.tds.metasolv.common.util.CommonUtilities;
import com.tds.metasolv.common.util.specific.PortAddressUtilities;
import junit.framework.TestCase;
public class SampleTest extends TestCase
{
PortAddressUtilities utils = null;
protected void setUp() throws Exception{
utils = PortAddressUtilities.getInstance(CommonUtilities.getInstance());
}
public void testGetPONPortNumber(){
String result = utils.getPONPortNumber("N10-1-2-3-4");
assertTrue("Expected 3, but got "+result +" in the result. ", result.equals("3"));
result = utils.getPONPortNumber("N10-1-2-3");
assertTrue("Expected 3, but got "+result +" in the result. ",result.equals("2"));
}
}
Problem is the src.dir you have defined
<property name="src.path" value="${project.local.directory}/SampleUnitTest/com"/>
The class you have created is in package com that means the class will be referenced using com.SampleTest
In order to fix this anomaly you need to add a src folder and move your com/Test.java to that folder so that the above defined src.path will be like
<property name="src.path" value="${project.local.directory}/SampleUnitTest/src"/>
And also change MyProject.classpath in build xml as below
<path id="MyProject.classpath">
<pathelement location="${lib.path}/ant-junit.jar" />
<pathelement location="${lib.path}/junit.jar" />
<pathelement location="${lib.path}/SampleUnitTest.jar" />
<pathelement location="${project.local.directory}/SampleUnitTest/target"/>
</path>

JUnit Test Suite and Ant Build "ClassNotFoundException"

I am using Eclipse Indigo. Not sure what is going on, but what I try to invoke the test suite from an ant build I get a ClassNotFoundException. However, if I right click on the JUnit Test class and run as Junit Test, it runs the tests fine. The error says that the file ./test/_ObservableSortUnitTests is not found. It gives the same error even if I give a full path to the file.
Here is my error:
Buildfile: /home/jason/Dev/ObservableSort/build.xml
Compile:
Test:
[junit] Testsuite: ./test/_ObservableSortUnitTests
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Caused an ERROR
[junit] ./test/_ObservableSortUnitTests
[junit] java.lang.ClassNotFoundException: ./test/_ObservableSortUnitTests
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:264)
[junit] at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[junit] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
[junit] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
BUILD FAILED
/home/jason/Dev/ObservableSort/build.xml:75: Test ./test/_ObservableSortUnitTests failed
Total time: 1 second
Here is my ant script (disclaimer: I am very new to ant):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="Build" name="CS 151 Project Build Script" >
<!--ANT 1.7 is required -->
<property name="home" value="." />
<property name="src.dir" value = "${home}/src" />
<property name="dest.dir" value="${home}/Release" />
<property name="dir.build" value="${home}/lib" />
<property name="dir.javadoc" value="${dest.dir}/Javadoc" />
<property name="dir.classes" value="${dest.dir}/Classes" />
<property name="dir.junit.reports" value="${dest.dir}/Reports" />
<property name="test.suite.dir" value="${home}/test" />
<property name="test.suite.class" value ="${test.suite.dir}/_ObservableSortUnitTests" />
<path id="build.class.path">
<fileset dir="${dir.build}">
<include name="*.jar" />
</fileset>
</path>
<path id="test.class.path">
<pathelement location="${junit.test.suite}" />
</path>
<target name="Clean" description="Deletes all old files">
<delete dir="${dir.javadoc}" />
<delete dir="${dir.classes}" />
<delete dir="${dir.junit.reports}" />
</target>
<target name="Prepare" description="Creates all necessary directories">
<mkdir dir="${dir.javadoc}" />
<mkdir dir="${dir.classes}" />
<mkdir dir="${dir.junit.reports}" />
</target>
<target name="Compile">
<javac srcdir="${src.dir}" destdir="${dir.classes}" includeantruntime="true">
<classpath refid="build.class.path" />
</javac>
</target>
<target name="Full" description="Executes all build targets">
<antcall target="Clean" />
<antcall target="Prepare" />
<antcall target="Compile" />
<antcall target="Test" />
<antcall target="Build" />
<antcall target="Javadoc" />
<antcall target="run" />
</target>
<target name="Build" description="Creates executable jar" depends="Clean, Prepare, Compile">
<jar destfile="${dest.dir}/ObservableSort.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="cs151.project1.ObservableSortTest"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="${home}/bin"/>
</jar>
</target>
<target name="Run" depends="Compile, Build">
<java jar="${dest.dir}/ObservableSort.jar" fork="true" />
</target>
<target name="Run with Unit Tests" depends="Compile, Build, Test">
<java jar="${dest.dir}/ObservableSort.jar" fork="true" />
</target>
<target name="Javadoc" description="Generate Javadoc" depends="Compile" >
<javadoc access="public" author="true" destdir="${dir.javadoc}" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="cs151.project1.sorters.insertionsort,cs151.project1.sorters.selectionsort,cs151.project1.Quantifiable,cs151.project1,cs151.project1.sorters.quicksort,cs151.project1.views" source="1.6" sourcepath="${src.dir}" splitindex="false" use="true" version="true"/>
</target>
<target name="Test" depends="Compile">
<junit>
<classpath refid="build.class.path" />
<classpath refid="test.class.path" />
<formatter type="plain" usefile="false" />
<test name="${test.suite.class}" haltonerror="true" />
</junit>
</target>
</project>
You are using the path to the testsuite class instead of the fully qualified name of the class.
Even though java classes are stored in filesystem folders, Java requires you to specify the package name dotted, not slashed.
Most likely the correct classname is test._ObservableSortUnitTests or maybe _ObservableSortUnitTests
If you specify the name of the test class in the junit task, then you need to use the fully qualifed class name, as Mark says. From JUnit task
<junit>
<test name="my.test.TestCase"/>
</junit>
If, however, you're using a batchtest, you can specify file names, but you need to add the .java on the end:
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
Stupid mistake: The JUnit classes were not yet compiled.
Solution:
Compile the project into a directory X
Compile the JUnit classes into a directory Y
Add the JUnit jar to directory Z
Inside the < test > tag, add directories X, Y, and Z to the classpath.

JUnit AntClassNotFoundException

I'm getting ClassNotFoundException when running "ant test" target.
All path and libraries exists including junit-4.8.1.jar located in lib folder.
<project name="progs" default="test" basedir=".">
<!--++++++++++ Properties ++++++++++-->
<property name="src-main" location="PC_Serie_3"/>
<property name="src-test" location="PC_Serie_3_Tests"/>
<property name="target" location="target"/>
<property name="target-classes" location="target/PC_Serie_3"/>
<property name="target-test-classes" location="target/PC_Serie_3_Tests"/>
<property name="target-test-reports" location="target/test-reports"/>
<path id="test.extlibs.class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
<!--++++++++++ Targets ++++++++++-->
<target name="init" description ="Creates the target folders">
<mkdir dir="${target-classes}"/>
<mkdir dir="${target-test-classes}"/>
<mkdir dir="${target-test-reports}"/>
</target>
<target name="clean" description="Removes the target folders" >
<delete includeEmptyDirs="true" failonerror="false" verbose="true" >
<fileset dir="${target}" defaultexcludes="false"/>
</delete>
</target>
<target name="compile-main" depends="init" description="Compiles the main source" >
<javac debug="true"
srcdir="${src-main}"
destdir="${target-classes}"
includeantruntime="false">
</javac>
</target>
<target name="compile-test" depends="compile-main" description="Compiles the test source" >
<javac debug="true"
debugLevel="source"
srcdir="${src-test}"
destdir="${target-test-classes}"
includeantruntime="true">
<classpath>
<pathelement location="${target-classes}"/>
<path refid="test.extlibs.class.path" />
</classpath>
</javac>
</target>
<target name="test" depends="compile-test" description="Runs the tests">
<echo>Running the junit tests...</echo>
<junit printsummary="yes" haltonfailure="true" showoutput="true" >
<classpath>
<pathelement location="${src-main}"/>
<pathelement location="${target-classes}"/>
<pathelement location="${target-test-classes}"/>
<path refid="test.extlibs.class.path" />
</classpath>
<batchtest fork="yes" todir="${target-test-reports}" >
<fileset dir="${src-test}">
<include name="**/*Test*.java"/>
</fileset>
<formatter type="xml"/>
<formatter type="plain" usefile="false" />
</batchtest>
</junit>
</target>
<target name="package" depends="test" description="Packages the main classes into a jar" >
<buildnumber />
<jar jarfile="${target}/library.jar" basedir="${target-classes}"/>
</target>
<!-- USAGE: ant deploy-app -->
<target name="deploy-lib" depends="package">
</target>
</project>
Console Output:
test:
[echo] Running the junit tests...
[junit] Running src.DocumentDBTests.DocumentDBv1Test
[junit] Testsuite: src.DocumentDBTests.DocumentDBv1Test
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] src.DocumentDBTests.DocumentDBv1Test
[junit] java.lang.ClassNotFoundException: src.DocumentDBTests.DocumentDBv1Te
st
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:169)
[junit]
I think the problem is that you are pointing junit to the source files and not the complied class files. Also, the fileset is including *Test*.java when it should be including *Test*.class
Try replacing your batchtest element with this:
<batchtest fork="yes" todir="${target-test-reports}" >
<fileset dir="${target-test-classes}">
<include name="**/*Test*.class"/>
</fileset>
<formatter type="xml"/>
<formatter type="plain" usefile="false" />
</batchtest>

Categories