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.
Related
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.
I am trying to compile and build the source files into a jar file using ant from the command line. (windows 7)
However I am getting the following error:
....\build.xml:43: Compile failed see the copiler error output for details
My build file is given below:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="jar.client">
<!--Auto generated ant build file-->
<property environment="env"/>
<property name="axis2.home" value="${env.AXIS2_HOME}"/>
<property name="project.base.dir" value="."/>
<property name="maven.class.path" value=""/>
<property name="name" value="addtaxihire_14cabs"/>
<property name="src" value="${project.base.dir}/src"/>
<property name="test" value="${project.base.dir}/test"/>
<property name="build" value="${project.base.dir}/build"/>
<property name="classes" value="${build}/classes"/>
<property name="lib" value="${build}/lib"/>
<property name="resources" value="${project.base.dir}/resources"/>
<property value="" name="jars.ok"/>
<path id="axis2.class.path">
<pathelement path="${java.class.path}"/>
<pathelement path="${maven.class.path}"/>
<fileset dir="${axis2.home}">
<include name="lib/*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${build}"/>
<mkdir dir="${classes}"/>
<mkdir dir="${lib}"/>
</target>
<target depends="init" name="pre.compile.test">
<!--Test the classpath for the availability of necesary classes-->
<available classpathref="axis2.class.path" property="stax.available" classname="javax.xml.stream.XMLStreamReader"/>
<available classpathref="axis2.class.path" property="axis2.available" classname="org.apache.axis2.engine.AxisEngine"/>
<condition property="jars.ok">
<and>
<isset property="stax.available"/>
<isset property="axis2.available"/>
</and>
</condition>
<!--Print out the availabilities-->
<echo message="Stax Availability= ${stax.available}"/>
<echo message="Axis2 Availability= ${axis2.available}"/>
</target>
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
</target>
<target depends="compile.src" name="compile.test" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}">
<src path="${test}"/>
<classpath refid="axis2.class.path"/>
</javac>
</target>
<target depends="pre.compile.test" name="echo.classpath.problem" unless="jars.ok">
<echo message="The class path is not set right! Please make sure the following classes are in the classpath 1. XmlBeans 2. Stax 3. Axis2 "/>
</target>
<target depends="jar.server, jar.client" name="jar.all"/>
<target depends="compile.src,echo.classpath.problem" name="jar.server" if="jars.ok">
<copy toDir="${classes}/META-INF" failonerror="false">
<fileset dir="${resources}">
<include name="*.xml"/>
<include name="*.wsdl"/>
<include name="*.xsd"/>
</fileset>
</copy>
<jar destfile="${lib}/${name}.aar">
<fileset excludes="**/Test.class" dir="${classes}"/>
</jar>
</target>
<target if="jars.ok" name="jar.client" depends="compile.src">
<jar destfile="${lib}/${name}-test-client.jar">
<fileset dir="${classes}">
<exclude name="**/META-INF/*.*"/>
<exclude name="**/lib/*.*"/>
<exclude name="**/*MessageReceiver.class"/>
<exclude name="**/*Skeleton.class"/>
</fileset>
</jar>
</target>
<target if="jars.ok" depends="jar.server" name="make.repo">
<mkdir dir="${build}/repo/"/>
<mkdir dir="${build}/repo/services"/>
<copy file="${build}/lib/${name}.aar" toDir="${build}/repo/services/"/>
</target>
<target if="jars.ok" depends="make.repo" name="start.server">
<java fork="true" classname="org.apache.axis2.transport.http.SimpleHTTPServer">
<arg value="${build}/repo"/>
<classpath refid="axis2.class.path"/>
</java>
</target>
<target if="jars.ok" depends="compile.test" name="run.test">
<path id="test.class.path">
<pathelement location="${lib}/${name}-test-client.jar"/>
<path refid="axis2.class.path"/>
<pathelement location="${classes}"/>
</path>
<mkdir dir="${build}/test-reports/"/>
<junit haltonfailure="yes" printsummary="yes">
<classpath refid="test.class.path"/>
<formatter type="plain"/>
<batchtest fork="yes" toDir="${build}/test-reports/">
<fileset dir="${test}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
</project>
and probably the 43rd-45th lines of the build file are:
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
Thanks in advance for any solution/suggestion.
The problem is not in the build.xml. It's in your java code. This implies the message:
Compile failed see the compiler error output for details
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>
<property name="wspace.home" value="${basedir}"/>
<property name="wspace.jars" value="D:\\softwares\\jars"/>
<property name="test.dest" value="${wspace.home}/build"/>
<property name="test.src" value="${wspace.home}/src"/>
<property name="test.reportsDir" value="D:\reports"/>
<path id="testcase.path">
<pathelement location="${test.dest}"/>
<fileset dir="${wspace.jars}">
<include name="*.jar"/>
</fileset>
</path>
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${wspace.jars}" includes="*.jar"/>
</path>
<pathconvert pathsep="."
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
</target>
<!-- all -->
<target name="all">
</target>
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean">
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<echo message="classpath-----: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.5"
classpath="${test.classpath}"
>
</javac>
</target>
<!-- build -->
<target name="build" depends="init">
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<path id="test.c">
<fileset dir="${wspace.jars}" includes="*.jars"/>
</path>
<!-- run -->
<target name="run">
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.reportsDir}" includes="**/*"/>
</delete>
<java jar="${wspace.jars}" fork="true" spawn="true"/>
<junit fork="yes" haltonfailure="no" printsummary="yes">
<classpath refid="testcase.path"/>
<!-- <classpath ="&{test.classpath}"/> -->
<batchtest todir="${test.reportsDir}" fork="true">
<fileset dir="${test.dest}">
<include name="TestSuite.class"/>
</fileset>
</batchtest>
<formatter type="xml"/>
<classpath refid="testcase.path"/>
</junit>
<junitreport todir="${test.reportsDir}">
<fileset dir="${test.reportsDir}">
<include name="TEST-.xml" />
</fileset>
<report todir="${test.reportsDir}"/>
</junitreport>
</target>
</project>
..........................................................................................
Reoprts are generated but they are blank, tried different solution mentioned is previou questions, but still not able to get.
I think testcase are not executed, do I have enetered something wrong in build.xml
Check below command prompt log
C:\Users\Vaibhav\Eclipse_Workspace\WebDriverTests>ant run
Buildfile: C:\Users\Vaibhav\Eclipse_Workspace\WebDriverTests
\build.xml
run:
[junitreport] Processing D:\reports\TESTS-TestSuites.xml to C:\Users\Vaibhav\AppData\Local\Temp\null526641997
[junitreport] Loading stylesheet jar:file:/C:/apache-ant-1.9.4/lib/ant-junit.jar
!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 524ms
[junitreport] Deleting: C:\Users\Vaibhav\AppData\Local\Temp\null526641997
BUILD SUCCESSFUL
Total time: 1 second
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.