Ant not picking up the Junit File with Junit Target - ant

I'm using the junit ant target for running the junits. When I run the Junit file separately the junit ran properly. When I tried with the ant script the junit test case was not executed. It showed me the error as,
Running x.SimpleTest
[junit] Testsuite: x.SimpleTest
[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] Caused an ERROR
[junit] x.SimpleTest
[junit] java.lang.ClassNotFoundException: x.SimpleTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[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)
[junit] Test x.SimpleTest FAILED
But the class is in the classpath. Please help me in debugging in this.
My build file is,
<property file="build.properties" />
<path id="libirary">
<fileset dir="${lib}">
<include name="**.*jar" />
</fileset>
</path>
<path id="applicationClassPath">
<path id="classpath1" refid="libirary" />
<pathelement id="classpath2" location="${build.classes.dir}" />
</path>
<path id="application.classpath">
<pathelement path="${java.class.path}" />
</path>
<path id="class.path">
<fileset dir="${build.classes.dir}" />
</path>
<property name="classPath" refid="applicationClassPath" />
<target name="clean">
<delete dir="${build.classes.dir}" />
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<target name="init" depends="clean">
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<target name="compile" depends="init">
<javac encoding="ISO-8859-1" destdir="${build.classes.dir}" srcdir="${src.dir}" debug="true" verbose="false">
<classpath>
<path refid="libirary" />
</classpath>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="${dist.dir}/${jar.filename}.jar">
<fileset dir="${build.classes.dir}" />
<zipfileset dir="${lib}" includes="**/*.jar" />
<manifest>
<attribute name="Main-Class" value="${main.file.name}" />
</manifest>
</jar>
</target>
<target name="junit" depends="compile">
<echo message="classpath= ${classPath}" />
<junit printsummary="yes" description="true" filtertrace="yes" showoutput="yes">
<formatter type="plain" usefile="false" />
<classpath>
<path refid="applicationClassPath" />
</classpath>
<test name="x.SimpleTest" filtertrace="true">
</test>
</junit>
</target>

It is resolved now. It was due to the classpath errors. Thank you all for the response.

Related

ClassNotFoundException with Ant and JUnit

Here is my code:
<project name="SampleProject" default="test-html" basedir=".">
<property name="ws.home" value="${basedir}" />
<property name="ws.jars" value="E:/jars" />
<property name="classes" value="${ws.home}/build" />
<property name="test.src" value="${ws.home}/src" />
<property name="test.reportsDir" value="c:/report" />
<!-- classpath -->
<path id="test.classpath">
<fileset dir="${ws.jars}" includes="*.jar" />
</path>
<!-- clean -->
<target name="clean" description="clean up">
<delete dir="${classes}" />
<echo message="Directory deleted..." />
</target>
<!-- create -->
<target name="create" description="create">
<mkdir dir="${classes}" />
<echo message="Directory created..." />
</target>
<!-- compile -->
<target name="compile" depends="clean,create">
<echo message="compiling..." />
<javac debug="true" srcdir="${test.src}" destdir="${classes}"
includeantruntime="false" classpathref="test.classpath" />
<echo message="compilation completed..." />
</target>
<!-- generating HTML-Report -->
<target name="test-html" depends="compile">
<junit fork="yes" printsummary="yes" haltonfailure="no">
<classpath refid="test.classpath" />
<batchtest fork="yes" todir="${test.reportsDir}">
<fileset dir="${classes}">
<include name="testcases/FirstTestCase.class" />
</fileset>
</batchtest>
<formatter type="xml" />
<classpath refid="test.classpath" />
</junit>
<junitreport todir="${test.reportsDir}">
<fileset dir="${test.reportsDir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.reportsDir}" />
</junitreport>
</target>
</project>
I am getting following problem in report generated with command prompt:
testcases.FirstTestCase
java.lang.ClassNotFoundException: testcases.FirstTestCase
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
And I am getting following error when the run build.xml in Eclipse:
Buildfile: E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build.xml
clean:
[delete] Deleting directory E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
[echo] Directory deleted...
create:
[mkdir] Created dir: E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
[echo] Directory created...
compile:
[echo] compiling...
[javac] Compiling 1 source file to E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
[echo] compilation completed...
test-html:
[junit] Running testcases.FirstTestCase
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Test testcases.FirstTestCase FAILED
[junitreport] Processing c:\report\TESTS-TestSuites.xml to C:\DOCUME~1\MUKESH~1\LOCALS~1\Temp\null1997459403
[junitreport] Loading stylesheet jar:file:/C:/Student%20Dvd/Student%20DVD/ANT/apache-ant-1.9.4-bin/apache-ant-1.9.4/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 2172ms
[junitreport] Deleting: C:\DOCUME~1\MUKESH~1\LOCALS~1\Temp\null1997459403
BUILD SUCCESSFUL
Total time: 5 seconds
I tried a lot to get out from here, but I didn't get solution. Help me.
It looks like the classes you compile are not on your test classpath.
The javac compiles to ${classes}, but that dir is not included in your definition of test.classpath.
Try adding the dir as a pathelement:
<path id="test.classpath">
<fileset dir="${ws.jars}" includes="*.jar" />
<pathelement path="${classes}" />
</path>

ant 1.9.2 + junit.. Class not found error

I am using JDK 1.7, eclipse 4.2.2, JUnit 4.8.1, ant 1.9.2, windows8 64 bit** When I am running my build.xml, all targets run fine but the target with name as run is not running fine.
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="AntExample" default="setclasspath" basedir=".">
<!-- Properties Initialization -->
<property environment="env"/>
<property name="ws.home" value="${basedir}" />
<property name="ws.jar" value="${basedir}/lib" />
<property name="test.dest" value="${basedir}/build" />
<property name="test.src" value="${basedir}/src" />
<property name="test.reportsDir" value="E:\Reports" />
<!-- Path Initialization -->
<path id="testcase.path">
<pathelement location="${ws.jar}"/>
<pathelement path="${classes}"/>
<fileset dir="${ws.jar}">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- Target Setclasspath -->
<target name="setclasspath" unless="testcase.path">
<path id="classpath_jars">
<fileset dir="${ws.jar}">
<include name="**/*.jar"/>
</fileset>
</path>
<pathconvert pathsep=";" property="testcase.path" refid="classpath_jars"/>
</target>
<!-- Target init -->
<target name="init" depends="setclasspath">
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows"/>
</condition>
</target>
<!-- Target clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- Target compile -->
<target name="compile" depends="init,clean">
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}">
<include name="**/*"/>
</fileset>
</delete>
<mkdir dir="${test.dest}"/>
<javac debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.7"
classpath="${testcase.path}"/>
</target>
<!-- Target run -->
<target name="run" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.reportsDir}">
<include name="**/*"/>
</fileset>
</delete>
<mkdir dir="${test.reportsDir}"/>
<java jar="${ws.jar}" fork="yes" spawn="yes"/>
<junit fork="yes" haltonfailure="no" printsummary="yes" >
<classpath refid="testcase.path" />
<batchtest todir="${test.reportsDir}" fork="true">
<fileset dir="${test.dest}">
<include name="anttestcase/Login.class"/>
</fileset>
</batchtest>
<formatter type="xml"/>
</junit>
<junitreport todir="${test.reportsDir}">
<fileset dir="${test.reportsDir}">
<include name="TESTS-*.xml"/>
</fileset>
<report todir="${test.reportsDir}"/>
</junitreport>
</target>
</project>`
When I am running my build with command prompt, i am getting the below error:
C:\Users\Ashish Rathore\workspace\AntExample>ant run
Buildfile: C:\Users\Ashish Rathore\workspace\AntExample\build.xml
run:
[junit] WARNING: multiple versions of ant detected in path for junit [junit]
jar:file:/H:/ant1.9/apache-ant-1.9.2-bin/apache-ant-1.9.2/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit]and
jar:file:/C:/Users/Ashish%20Rathore/workspace/AntExample/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit]Running anttestcase.Login [junit]Tests run: 1, Failures: 0,
Errors: 1, Skipped: 0, Time elapsed: 0 sec [junit] Test
anttestcase.Login FAILED [junitreport] Processing
E:\Reports\TESTS-TestSuites.xml to C:\Users\ASHISH~1\Ap
pData\Local\Temp\null1158960870 [junitreport] Loading stylesheet
jar:file:/H:/ant1.9/apache-ant-1.9.2-bin/apache
-ant-1.9.2/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/j
unit-frames.xsl [junitreport] Transform time: 1223ms [junitreport]
Deleting: C:\Users\ASHISH~1\AppData\Local\Temp\null1158960870
BUILD SUCCESSFUL Total time: 5 seconds
Error message in TEST-anttestcase.Login.xml in my Reports
error type="java.lang.ClassNotFoundException" message="anttestcase.Login">
java.lang.ClassNotFoundException: anttestcase.Login at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:423) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
java.lang.ClassLoader.loadClass(ClassLoader.java:356) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:188)
My Login.java test case
package anttestcase;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
public class Login {
#Rule public ErrorCollector errCollector=new ErrorCollector();
#BeforeClass public static void setUp() throws Exception {
System.out.println("opening Url");
}
#AfterClass public static void tearDown() throws Exception {
}
#Test public void enterCredentials() {
try {
Assert.assertEquals("A", "B");
System.out.println("Enter Username and Password");
} catch(Throwable t) {
errCollector.addError(t);
System.out.println("Caught");
}
}
#Test public void authenticityCheck() {
System.out.println("Login Successfully");
}
}
You must add the compiled classes to the class path of the junit task.
<junit fork="yes" haltonfailure="no" printsummary="yes" >
<classpath>
<path refid="testcase.path">
<pathelement location="${test.dest}"/>
</classpath>
<batchtest todir="${test.reportsDir}" fork="true">
<fileset dir="${test.dest}">
<include name="anttestcase/Login.class"/>
</fileset>
</batchtest>
<formatter type="xml"/>
</junit>
On Github there's an example ant project, which uses JUnit: https://github.com/mplacona/java-junit-template-project Have a look at it's build.xml.

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.

Junit test integrated with Ant Failed with ClassNotFoundException

I have JUnit tests on my project that run correctly with Eclipse.
So, now I try to integrate these tests with an ant task. To make that I make the following ant script :
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="unit-test" depends="compile-tests">
<junit printsummary="false" fork="off" haltonfailure="true">
<classpath refid="classpath-test" />
<formatter type="brief" usefile="false" />
<test name="com.test.MyTest" />
<!--<batchtest todir="${reports.dir}" >
<fileset dir="${testclasses.home}" >
<exclude name="**/AllTests*"/>
<include name="**/*Test.class" />
</fileset>
</batchtest>-->
</junit>
</target>
The directory ${libtest.hom} contains junit-4.8.1.jar and hamcrest-core-1.1.jar.
When I launch the following command : ant unit-test, the execution of the MyTest fails with the following output :
unit-test:
[junit] Testsuite: com.test.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] com.test.MyTest
[junit] java.lang.ClassNotFoundException: com.test.MyTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit]
[junit]
It's very strange because com.test.MyTest is well in the classpath pointed for my task junit in ant script. Someone would have and idea to solve this problem ?
Thanks for your help.
Sylvain.
The ${testclasses.home} directory is nowhere on a classpath for <junit> task.
I think this is where class file for com.test.MyTest lives.
Here is modified unit-test target:
<target name="unit-test" depends="compile-tests">
<junit printsummary="false" fork="off" haltonfailure="true">
<classpath>
<path refid="classpath-test"/>
<fileset dir="${testclasses.home}"/>
</classpath>
<formatter type="brief" usefile="false" />
<test name="com.test.MyTest" />
<!--<batchtest todir="${reports.dir}" >
<fileset dir="${testclasses.home}" >
<exclude name="**/AllTests*"/>
<include name="**/*Test.class" />
</fileset>
</batchtest>-->
</junit>
</target>

Resources