Ant jUnit WebDriver - NoClassDefFoundError - ant

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.

Related

Use JUnit with Jenkins & Ant Project

I am running Junit Tests on Jenkins via ant.
I can received the build success message in Jenkins, but JUnit result is error.
java.lang.ClassNotFoundException: JUnitTest
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
This is my build.xml.
<?xml version="1.0"?>
<project name="testProject" default="main" basedir=".">
<target name="init">
<property name="src" location="src" />
<property name="bin" location="bin" />
<property name="lib" location="WebContent/WEB-INF/lib" />
<property name="war.name" location="testProject.war" />
<property name="web" location="WebContent" />
<mkdir dir="${bin}" />
<tstamp>
<format property="DSTAMP" pattern="yyyyHHdd" />
<format property="TSTAMP" pattern="HHmm" />
</tstamp>
</target>
<target name="compile" depends="init">
<fileset dir="WebContent/WEB-INF/lib" includes="*.jar" />
</path>
<javac srcdir="src/test" destdir="${bin}" debug="on" includeantruntime="false">
<classpath refid="junit.jar"/>
<include name="*.java"/>
</javac>
<javac srcdir="src/testProject" destdir="${bin}" debug="on" includeantruntime="false">
<include name="*.java"/>
</javac>
</target>
<target name="junit4" depends="compile">
<delete dir="report" />
<mkdir dir="report" />
<junit printsummary="on" fork="false" haltonfailure="false">
<classpath refid="junit.jar"/>
<formatter type="xml"/>
<batchtest todir="report">
<fileset dir="src/test" includes="**/JUnit*.java" />
</batchtest>
</junit>
<junitreport todir="report">
<fileset dir="report" includes="TEST-*.xml" />
<report format="frames" todir="report"/>
</junitreport>
</target>
<target name="main" depends="clean,war">
<java classpath="${bin}" classname="testProject.HelloWorld">
</java>
</target>
<target name="war" depends="compile,junit4">
<war destfile="${bin}/${DSTAMP}.war" webxml="${web}/WEB-INF/web.xml">
<fileset dir="${web}">
<include name="**/*.*" />
<exclude name="WEB-INF/web.xml" />
</fileset>
</war>
</target>
<target name="clean">
<delete dir="${bin}" />
</target>
</project>
I've reviewed other similar issues, but it didn't work.
Why can't it find the Class?

Jacoco coverage with Ant throws java.lang.instrument.IllegalClassFormatException: Error while instrumenting class

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.

Groovy-TestNG-Ant running error

I've faced with next problem.
Trying to create pilot test-project using Groovy, TestNG, ant.
If I run tests from my IDE (Eclipse) - all is ok.
But when I try run tests using ant - not easy for me problem. Could you help me?
build.xml is present below.
Compile target - pass ok, but on runTest target I see problem:
"Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject"
<project basedir="." default="runTest" name="Ant file for TestNG">
<property name="src" location="src" />
<property name="build" location="build" />
<property name="libs" location="lib" />
<path id="class.path">
<pathelement location="${libs}/testng.jar" />
<pathelement location="lib/testng.jar"/>
<pathelement location="${build}" />
</path>
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="lib/groovy-all.jar"/>
<pathelement location="lib/testng.jar"/>
</classpath>
</taskdef>
<target name="runTest" depends="compile">
<mkdir dir="testng_output"/><!-- Create the output directory. -->
<testng outputdir="testng_output" classpathref="class.path">
<xmlfileset dir="." includes="testng.xml"/>
</testng>
</target>
<target name="compile">
<delete dir="build"/>
<mkdir dir="build"/>
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc" classpath="lib/groovy-all.jar"/>
<groovyc srcdir="src" destdir="./build">
<classpath>
<pathelement path="lib/groovy-all.jar"/>
<pathelement path="lib/testng.jar"/>
</classpath>
<javac source="1.7" target="1.7" debug="off" />
</groovyc>
</target>
</project>
And this is testng.xml
<suite name="My Test Suite" parallel="methods" thread-count="5">
<test name="My Test">
<classes>
<class name="test1" />
</classes>
</test>
</suite>

HTML Report for JUNIT by ANT

How can I generate HTML reports from JUnit using Ant when there are test failures?
The reports are generated when there are no failures.
Also, how can we define our own XSLT for the report generation?
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Example" default="all" basedir=".">
<property name="project_name" value="junitSamples" />
<property name="src" location="src" />
<property name="build" location="build/classes" />
<property name="lib" location="lib" />
<property name="reports" location="reports" />
<target name="init" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${lib}" />
<mkdir dir="${reports}" />
<mkdir dir="${reports}/raw/" />
<mkdir dir="${reports}/html/" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" description="compile the source code ">
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="clean">
<delete dir="build" />
<delete dir="${reports}" />
</target>
<target name="run-tests" depends="compile">
<junit printsummary="yes" haltonfailure="yes" showoutput="yes">
<classpath>
<pathelement path="${build}" />
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml" />
<fileset dir="${src}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${reports}\html\" />
</junitreport>
</target>
<target name="all" depends="clean, test" />
</project>
To specify your own stylesheets, use the "styledir" attribute:
<report styledir="${resources}/junit" format="..." todir="..." />
As noted, you must use the "junit-noframes.xsl" stylesheet name.
JUnit report docs.
You set haltonfailure="yes" it means that if one test fails build operation will stop.
This is from the ant documentation:
"Stop the build process if a test fails (errors are considered failures as well)."
Read more here- https://ant.apache.org/manual/Tasks/junit.html

Junit Test Not Deploying from Ant Script

I have an ant script that I am trying to run my junit test from. It runs all but the junit portion of the ant with no errors. Anyone know why it's not hitting this portion of my script?
<?xml version="1.0"?>
<property file="build.properties" />
<property name="test.class.name" value="edu.psu.ist.probability.TestProbability" />
<path id="test.classpath">
<pathelement location="${classes}" />
<pathelement location="/lib/junit.jar" />
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef resource="checkstyletask.properties" classpath="${checkstyle}" />
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
<classpath>
<pathelement path="${findbugs.jar}" />
</classpath>
</taskdef>
<!--
Encapsulates the process of starting up program.
-->
<target name="run" depends="build">
<echo>-----------RUN-----------</echo>
<java classname="${main}" fork="true">
<!-- fork needed for running on OS X -->
<classpath>
<pathelement location="${jar}" />
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</java>
</target>
<!--
Encapsulates the process of packaging (jarring) program.
-->
<target name="build" depends="document">
<echo>---------BUILD--------</echo>
<jar destfile="${jar}" basedir="${classes}" />
</target>
<!--
Encapsulates the process of generating javadoc for program.
-->
<target name="document" depends="findbugs">
<echo>-------DOC--------</echo>
<javadoc sourcepath="${src}" defaultexcludes="yes" destdir="${docs}" executable="C:\Program Files (x86)\Java\jdk1.6.0_20\bin\javadoc.exe" author="true" version="true" use="true" windowtitle="Decisions API">
</javadoc>
</target>
<!--
Encapsulates the process of executing findbugs against program.
-->
<target name="findbugs" depends="style">
<echo>----------FINDBUGS----------</echo>
<findbugs home="${findbugs}" output="html" outputFile="${findbugs.output}" failOnError="true" stylesheet="fancy.xsl">
<auxClasspath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</auxClasspath>
<sourcePath path="${src}"/>
<class location="${classes}" />
</findbugs>
</target>
<!--
Encapsulates the process of style-checking program.
-->
<target name="style" depends="compile">
<echo>---STYLE---</echo>
<checkstyle config="${checkstyleconfig}" classpath="${classes}">
<fileset dir="${src}" includes="**/*.java"
excludes="**/DecisionsProgram.java, **/*Test.java" />
</checkstyle>
</target>
<!--
Encapsulates the process of compiling program.
-->
<target name="compile" depends="clean">
<echo>------------COMPILE--------</echo>
<javac destdir="${classes}">
<src path="${src}" />
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<!--
Encapsulates the process of cleaning up program directory structure.
-->
<target name="clean">
<!-- make sure dirs exist first, to prevent error -->
<mkdir dir="${classes}" />
<mkdir dir="${dist}" />
<mkdir dir="${docs}" />
<mkdir dir="${reports}" />
<!-- now delete them -->
<delete dir="${classes}" />
<delete dir="${dist}" />
<delete dir="${docs}" />
<delete dir="${reports}" />
<!-- now recreate them so they can be used -->
<mkdir dir="${classes}" />
<mkdir dir="${dist}" />
<mkdir dir="${docs}" />
<mkdir dir="${reports}" />
</target>
<target name="test" depends="compile">
<echo>-------JUNIT----------</echo>
<junit fork="yes" haltonfailure="yes">
<test name="${test.class.name}" />
<formatter type="plain" usefile="false" />
<classpath refid="test.classpath" />
</junit>
</target>
There is no target which is invoking "test".
One possibility is to replace
<target name="style" depends="compile">
with
<target name="style" depends="test">

Resources