java.lang.NoClassDefFoundError when trying to run JUnit tests from ANT - testing

I'm having problems with a java.lang.NoClassDefFoundError when trying to run my JUnit tests from ANT using the runtestsreport task in thefollowing build.xml; I know the other tasks work, it's just the actual running of the tests that's the problem.
I've tried lots of adjustments based on questions posted on here but I can't fix it; what I really need is a dev to pair with but they've all gone home for the day.
Can anyone help?
The build.xml file is
<property name="build" location="C:\<path on my machine>\workspace\ESS\build"/>
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\tests"/>
<property name="frameworkbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\framework"/>
<property name="rawtestreports" location="C:\<path on my machine>\workspace\ESS\reports\raw"/>
<property name="htmltestreports" location="C:\<path on my machine>\workspace\ESS\reports\html"/>
<property name="reports" location="C:\<path on my machine>\workspace\ESS\reports"/>
<property name="src" location="C:\<path on my machine>\workspace\ESS\src"/>
<property name="testsrc" location="C:\<path on my machine>\workspace\ESS\src\esstestapp\tests"/>
<property name="seleniumtools" location="C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2"/>
<property name="junittools" location="C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${rawtestreports}"/>
<mkdir dir="${htmltestreports}"/>
</target>
<path id="tools.classpath">
<fileset dir="${seleniumtools}" includes="**/*.jar"/>
<fileset dir="${junittools}" includes="**/*.jar"/>
</path>
<target name="compile" depends="init" description="compile the source" >
<javac srcdir="${src}" destdir="${build}" classpathref="tools.classpath" includeantruntime="false"/>
</target>
<target name="frameworkjar" depends="compile">
<jar destfile="${frameworkbuild}/jar/framework.jar" basedir="${frameworkbuild}" />
</target>
<target name="testsjar" depends="compile">
<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
</target>
<path id="test.classpath">
<fileset dir="${seleniumtools}" includes="**/*.jar"/>
<fileset dir="${junittools}" includes="**/*.jar"/>
<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
</path>
<target name="runtests" depends="compile, frameworkjar, testsjar" description="runs the tests" >
<junit printsummary="yes" haltonfailure="no" showoutput="yes" fork="no">
<classpath refid="test.classpath" />
<batchtest fork="yes" todir="${rawtestreports}">
<formatter type="xml"/>
<fileset dir="${testsrc}">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="runtestsreport" depends="runtests">
<junitreport todir="${reports}">
<fileset dir="${rawtestreports}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${htmltestreports}" />
</junitreport>
</target>
<target name="clean" description="Clean up">
<delete dir="${build}"/>
<delete dir="${reports}"/>
</target>
I get this error (and this type of error for each test class)
Credentials (wrong name: esstestapp/tests/Credentials)
java.lang.NoClassDefFoundError: Credentials (wrong name: esstestapp/tests/Credentials)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)...
I've ran ant in -verbose mode to check the classpath out but I can't see the problem (even though I know that it's staring me in the face)
[junit] Implicitly adding C:\Program Files\apache-ant-1.8.2\lib\ant-launcher
.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\apache-ant-1
.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-junit4.jar to C
LASSPATH
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] 'C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2\junit.jar;C:\User
s\waltersj\workspace\ESS\tools\junit-4.8.2\junitsrc.jar;C:\<path on my machine>\worksp
ace\ESS\tools\selenium-2.0rc2\apache-mime4j-0.6.jar;C:\<path on my machine>\workspace\
ESS\tools\selenium-2.0rc2\bsh-1.3.0.jar;C:\<path on my machine>\workspace\ESS\tools\se
lenium-2.0rc2\cglib-nodep-2.1_3.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-codec-1.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2
.0rc2\commons-collections-3.2.1.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-io-2.0.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.
0rc2\commons-lang-2.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
commons-logging-1.1.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
cssparser-0.9.5.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\guava-
r09.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\hamcrest-all-1.1.j
ar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\htmlunit-2.8.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\htmlunit-core-js-2.8.jar;C:\User
s\waltersj\workspace\ESS\tools\selenium-2.0rc2\httpclient-4.0.2.jar;C:\Users\wal
tersj\workspace\ESS\tools\selenium-2.0rc2\httpcore-4.0.1.jar;C:\<path on my machine>\w
orkspace\ESS\tools\selenium-2.0rc2\httpmime-4.0.1.jar;C:\<path on my machine>\workspac
e\ESS\tools\selenium-2.0rc2\jcommander-1.13.jar;C:\<path on my machine>\workspace\ESS\
tools\selenium-2.0rc2\jna.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0
rc2\json-20080701.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\juni
t-dep-4.8.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\nekohtml-1
.9.14.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\sac-1.3.jar;C:\U
sers\waltersj\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2-srcs.jar;
C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\serializer-2.7.1.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\testng-6.0.1-nobsh-noguice.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\xalan-2.7.1.jar;C:\Users\wa
ltersj\workspace\ESS\tools\selenium-2.0rc2\xercesImpl-2.9.1.jar;C:\Users\walters
j\workspace\ESS\tools\selenium-2.0rc2\xml-apis-1.3.04.jar;C:\<path on my machine>\work
space\ESS\build\esstestapp\tests\jar\tests.jar;C:\<path on my machine>\workspace\ESS\b
uild\esstestapp\framework\jar\framework.jar;C:\<path on my machine>\workspace\ESS;C:\P
rogram Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\apache-ant-1.8.2\lib\
ant-launcher.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\
apache-ant-1.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-jun
it4.jar'
Any eyes over this to help me get the tests running would be greatly appreciated.
Thanks
James
Update
I observe that if I edit tests.classpath to remove the following 2 lines then the error I see switches to a ClassNotFound which makes sense as it has no references to the .jar files that contain the classes.
<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
With the above 2 lines present in the tests.classpath is happily finds them but with the wrong name and it's that error I don't know how to fix.

Problem caused by the following jar command:
<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
The testsbuild property should be set to
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build"/>
Explanation
The file Credentials.class was compiled as the class esstestapp.tests.Credentials. It needs to be found on the classpath as the file:
esstestapp/tests/Credentials.class

Related

Ant Test Compile Crashes

I am trying to compile a class named TestsVorgabe wich represents a testsuite including five other tests. It works fine with normal JUnit testing, but when i try to compile it with ant i get an
error: cannot find symbol
Project structure like this:
project/src => Classes
project/test => TestClasses
To make sure i really get the correct .jars and include them i have two ways delivering them to Ant. My Ant Buildfile looks like this:
<property name="src.dir" location="src"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="dist"/>
<property name="docs.dir" location="docs"/>
<property name="test.dir" location="test"/>
<property name="buildtest.dir" location="buildtest"/>
<path id="src.path">
<pathelement path="src/"/>
</path>
<path id="compile.path">
<path refid="src.path"/>
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
</path>
<path id="unit.test.path">
<path refid="compile.path"/>
<pathelement path="test/"/>
</path>
<!--Löscht die erstellten Verzeichnisse und Sourcen-->
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${buildtest.dir}"/>
<delete dir="${dist.dir}"/>
<delete dir="${docs.dir}"/>
</target>
<!--Erzstellt die Verzeichnisstruktur-->
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${buildtest.dir}"/>
<mkdir dir="${dist.dir}"/>
<mkdir dir="${docs.dir}"/>
<mkdir dir="${dist.dir}/dist2"/>
</target>
<!--Kompiliert die Programm-Klassen-->
<target name="compile">
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="compile.path"/>
</javac>
</target>
<target name="compileUnitTests" depends="compile">
<javac srcdir="test/" destdir="${buildtest.dir}">
<classpath refid="unit.test.path"/>
</javac>
</target>
<target name="runUnitTests" depends="compile">
<junit printsummary="yes" haltonfailure="no">
<jvmarg value="-Dfile.encoding=UTF-8"/>
<classpath refid="unit.test.path"/>
<formatter type="plain"/>
<batchtest fork="yes" todir="${buildtest.dir}">
<fileset dir="${test.dir}">
<include name="**/TestsVorgabe.java"/>
<exclude name="**/AllTests.class"/>
<exclude name="**/*$*.class"/>
</fileset>
</batchtest>
</junit>
</target>
I have tried many ways to inculde the .jars and many ways of compiling the tests, but nothing seemed to work so i ended up with this (do not work too).
You compile your sources with javac into the ${build.dir} but you doesn't add that to the classpath of the junit task, this is an example from the documentation:
<classpath>
<pathelement location="${build.tests}"/>
<pathelement path="${java.class.path}"/>
</classpath>
Or check this example(in the example: ${web.classes.dir}).
Try to add build.dir to the junit task classpath, I hope this helps!

ant junit selenium wont run in jenkins

I have junit tests that use selenium to test web server.
When i run the tests using ant from command line, everything is working fine, browser gets opened and tests are going as planed.Browser gets open and i can see tests running.
Recently ive tried to add automatic tests as part of Ci cycle running on jenkins.
I run it as ant build command.
I can see that ant is executing properly (test classes are built i can see output from tests to console) but browser window never gets opened and test fails because of it. here is my ant file
<?xml version="1.0"?>
<project name="JUNIT" default="main" basedir="../../project" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="api.dir" location="src/java" />
<property name="build.api.dir" location="target/classes" />
<property name="test.dir" location="src/test/java" />
<property name="build.test.dir" location="target" />
<!-- Variables used for JUnit testin -->
<property name="test.report.dir" location="testreport" />
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="api.class.path">
<pathelement location="${build.api.dir}" />
</path>
<artifact:dependencies cacheDependencyRefs="true" pathId="pomdeps.path">
<pom file="pom.xml"/>
</artifact:dependencies>
<target name="clean">
<delete dir="${test.report.dir}" />
<delete dir="${build.api.dir}" />
<delete dir="${build.test.dir}" />
</target> <!-- Creates the build, docs and dist directory-->
<target name="makedir">
<echo message="Make dir"/>
<mkdir dir="${build.test.dir}" />
<mkdir dir="${build.api.dir}" />
<mkdir dir="${test.report.dir}" />
</target> <!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<echo message="Compile"/>
<javac srcdir="${api.dir}" destdir="${build.api.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" />
<classpath refid="libs.class.path" /-->
<classpath refid="pomdeps.path" />
</javac>
<javac srcdir="${test.dir}" destdir="${build.test.dir}" includeantruntime="false">
<!--classpath refid="junit.class.path" /-->
<classpath refid="api.class.path" />
<classpath refid="pomdeps.path" />
</javac>
</target>
<!-- Run the JUnit Tests --> <!-- Output is XML, could also be plain-->
<echo message="Classes folder ${build.test.dir}"/>
<target name="junit" depends="compile" >
<echo message="junit"/>
<junit printsummary="on" fork="false" haltonfailure="no" showoutput="true">
<classpath refid="pomdeps.path" />
<classpath>
<pathelement location="${build.test.dir}"/>
<pathelement location="${build.api.dir}"/>
</classpath>
<formatter usefile="false" type="plain"/>
<batchtest fork="no" todir="${test.report.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="main" depends="compile, junit">
<description>Main target</description>
</target>
</project>
Jenkins is 1.591 i installed it with default parameters as windows installation downloaded from their site.
Can it be something wrong with jenkins? Do i miss something?
As i mentioned earlier the problem was lack of UI permissions for Jenkins server.
1.configure Jenkins to run as service and make it login with real user name
2.Make sure that windows host that runs Jenkins logs on automaticaly after restart.

How to take back up of existing projects of remote server using ant

I am deploying my application using ant build script mention below. my application gets deployed on specified server mentioned in my ant script. but i want to take back of existing deployed project on remote server before i deployed new project. Here is my ant script.
<?xml version="1.0"?>
<project name="xyz" basedir="." default="deploy">
<!-- Read values from buildscript properties file -->
<property file="buildscript.properties" />
<!-- set global variable for this build -->
<property name="build" value="${basedir}/build"/>
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="war.dir" value="${build.dir}/war"/>
<!-- Configure the context path for this application -->
<property name="path" value="/xyz"/>
<!-- set class path -->
<path id="compile.classpath">
<fileset dir="WebContent/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${tomcatDir}">
<include name="*.jar"/>
</fileset>
</path>
<!-- Configure the custom Ant tasks for the Tomcat Server Manager -->
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask">
<classpath refid="compile.classpath" />
</taskdef>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask">
<classpath refid="compile.classpath" />
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
<classpath refid="compile.classpath" />
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
<classpath refid="compile.classpath" />
</taskdef>
<!-- delete build directory -->
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<!-- create classes, war directory in build -->
<target name="create" depends="clean">
<mkdir dir="${classes.dir}"/>
<mkdir dir="${war.dir}"/>
</target>
<!-- compile source code and put classes in classes dir -->
<target name="compile" depends="create">
<copy todir="${classes.dir}" overwrite="true">
<fileset dir="src" excludes="*.java"/>
</copy>
<!-- for replacing cfg file to deploy on stagging-->
<copy todir="${classes.dir}" overwrite="true">
<fileset dir="${configurationFileDir}">
<include name="*.xml"/>
</fileset>
</copy>
<javac destdir="${classes.dir}" srcdir="${src.dir}" debug="true">
<classpath refid="compile.classpath"/>
</javac>
</target>
<!-- create war file -->
<target name="war" depends="compile">
<war destfile="${war.dir}/xyz.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent"/>
<classes dir="build/classes"/>
</war>
</target>
<!-- Deploy war file -->
<target name="deploy" description="Install web application"
depends="war">
<deploy url="${url}" username="${username}" password="${password}"
path="${path}" war="file:${war.dir}/xyz.war"/>
</target>
<!-- Start apllication -->
<target name="start" description="start application in tomcat">
<start url="${url}" username="${username}" password="${password}" path="${path}"/>
</target>
<!-- Undeploy war file -->
<target name="undeploy" description="Remove web application" >
<undeploy url="${url}" username="${username}" password="${password}"
path="${path}"/>
</target>
<!-- Stop apllication -->
<target name="stop" description="stop application in tomcat">
<stop url="${url}" username="${username}" password="${password}" path="${path}"/>
</target>
</project>
The manager documentation describes a special tag attribute that can be used to redeploy your application:
URL parameters include:
update: When set to true, any existing update will be undeployed first....
tag: Specifying a tag name, this allows associating the deployed webapp
with a tag or label. If the web application is undeployed, it can be
later redeployed when needed using only the tag.
Unfortunately, the ANT task documentation does not describe any support for this attribute...
Interestingly, the Maven plugin does support this, but switching build tool would be a rather extreme solution to this problem :-)

TestNG Selenium Test fails in Ant through command prompt

I am using Selenium testNG in eclipse to run my test cases.The testNg.xml file executes the test correctly.
But when I try to run in command prompt through ant the build is success but the test fails.
one peculiar thing happening is : when the driver i use to test is HtmlUnitdriver build is successful and also the test passes . But it fails when i use other browsers like firefox, chrome, explorer in my test case.
the build.xml i use is:
<project name="Automation" default="clean" basedir=".">
<property name="build.dir" value="${basedir}/build"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/src"/>
<target name="setClassPath">
<path id="classpath_jars">
<pathelement path="${basedir}/" />
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>
<target name="loadTestNG" depends="setClassPath">
<taskdef resource="testngtasks" classpath="${test.classpath}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}"/>
<tstamp>
<format property="timestamp" pattern="dd-MM-yyyy_(HH-mm-ss)"/>
</tstamp>
<property name="build.log.dir" location="${basedir}/buildlogs"/>
<mkdir dir="${build.log.dir}"/>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
<echo message="build logged to ${build.log.filename}"/>
</target>
<target name="clean">
<echo message="deleting existing build directory"/>
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="clean,init,setClassPath,loadTestNG">
<echo message="classpath:${test.classpath}"/>
<echo message="compiling.........."/>
<javac destdir="${build.dir}" srcdir="${src.dir}" classpath="${test.classpath}"/>
</target>
<target name="runTests" depends="compile">
<testng classpath="${test.classpath}:${build.dir}">
<xmlfileset dir="${basedir}" includes="testNg.xml"/>
</testng>
</target>
</project>
I appreciate any help.

Unexpected attribute "BuildInParallel" on element <msbuild>

I am trying to use the BuildInParallel option on MsBuild.
I have an NAnt & NAntContrib script e.g.
<project xmlns="http://nant.sf.net/release/0.90/nant.xsd" name="Css Manager Solution Build" ToolsVersion="3.5" default="deploy">
<target name="clean" description="Delete all previously compiled binaries.">
<delete>
<fileset>
<include name="**/bin/**" />
<include name="**/obj/**" />
<include name="**/*.suo" />
<include name="**/*.user" />
</fileset>
</delete>
</target>
<target name="deploy" description="Build and deploy all targets.">
<msbuild project="CssManager.sln" BuildInParallel="true">
<property name="Configuration" value="${configuration}"/>
<property name="OutDir" value="${bin.output.dir}"/>
</msbuild>
</target>
</project>
but I get this error message:
Unexpected attribute "BuildInParallel" on element <msbuild>
Please advise?
The MSBuild task of nant-contrib doesn't have a BuildInParallel attribute. You'll have to use the Maxcpucount command line argument.
<target name="deploy" description="Build and deploy all targets.">
<msbuild project="CssManager.sln" BuildInParallel="true">
<property name="Configuration" value="${configuration}"/>
<property name="OutDir" value="${bin.output.dir}"/>
<arg value="/maxcpucount:${environment::get-variable('NUMBER_OF_PROCESSORS')}"/>
</msbuild>
</target>
More info on MaxCpuCount
More info on msbuild task