1
votes

Here I am integrating testng.xml in build.xml file. But when I try to run build.xml, I get "Build failed" error. I don’t know why. Please suggest me if you have some ideas. Also please review my execution steps for your better understanding. If any mistake is there, please let me know.

Execution steps:

  1. Removing bin,docs,jar folders if already there
  2. Create new folder for bin,docs,jar
  3. set classpath for external jars.
  4. Before setting classpath, I placed "testng-6.8.5.jar" in lib folder (currently am using only one jar file)
  5. Compile the source code and copy the .class files into bin folder
  6. Run specific "target" to get test report from TestNG

Build.xml

<?xml version="1.0"?>
<project name="AntBasics" default="main" basedir=".">
    <description>Running TestNG with Ant</description>

    <!-- Properties Declaration -->

    <property name="src.dir" location="src" />
    <property name="test.dir" location="src" />
    <property name="lib.dir" location="lib" />
    <property name="build.dir" location="bin" />
    <property name="dist.dir" location="dist" />
    <property name="docs.dir" location="docs" />
    <property name="docs.dir" location="docs" />
    <property name="test.report.dir" location="test-output" />
    <property name="resources.dir" location="testng.xml" />


    <!-- set classpath -->
    <path id="build.classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!-- Clean bin,dist,docs,test-output -->
    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${dist.dir}" />
        <delete dir="${docs.dir}" />
        <delete dir="${test.report.dir}" />
    </target>

    <!-- Create build,dist,docs,test-output-->
    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${dist.dir}" />
        <mkdir dir="${docs.dir}" />
    </target>

    <!-- Compile Java code-->
    <target name="compile" depends="clean,makedir">
        <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
    </target>

    <!-- Create Javadocs-->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </javadoc>
    </target>

    <!-- Make jar using Java class files-->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\myna.ant.test.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-class" value="myPack2.TestNGMain" />
            </manifest>
        </jar>
    </target>

    <!-- Define task for TestNG-->
    <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="build.classpath">
        <classpath location="lib/testng-6.8.5.jar" />
    </taskdef>

    <!-- Mention the TestNG Task and Make Test Reports-->
    <target name="runTest" depends="compile,jar,docs">
        <mkdir dir="${test.report.dir}" />
        <testng outputdir="${test.report.dir}" haltOnFailure="true" classpathref="build.classpath">
            <xmlfileset dir="." includes="testng.xml" />
        </testng>
    </target>

    <!-- Default Main Target-->
    <target name="main" depends="runTest">
        <description>Main Application</description>
    </target>

</project>

In console

Buildfile: /home/vaav/workspace/AntBasics/build.xml
clean:
   [delete] Deleting directory /home/vaav/workspace/AntBasics/bin
   [delete] Deleting directory /home/vaav/workspace/AntBasics/dist
   [delete] Deleting directory /home/vaav/workspace/AntBasics/docs
   [delete] Deleting directory /home/vaav/workspace/AntBasics/test-output
makedir:
    [mkdir] Created dir: /home/vaav/workspace/AntBasics/bin
    [mkdir] Created dir: /home/vaav/workspace/AntBasics/dist
    [mkdir] Created dir: /home/vaav/workspace/AntBasics/docs
compile:
    [javac] Compiling 3 source files to /home/vaav/workspace/AntBasics/bin
jar:
      [jar] Building jar: /home/vaav/workspace/AntBasics/dist/myna.ant.test.jar
docs:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source file /home/vaav/workspace/AntBasics/src/myPack/Test1.java...
  [javadoc] Loading source file /home/vaav/workspace/AntBasics/src/myPack2/TestMain.java...
  [javadoc] Loading source file /home/vaav/workspace/AntBasics/src/myPack2/TestNGMain.java...
  [javadoc] Constructing Javadoc information...
  [javadoc] /home/vaav/workspace/AntBasics/src/myPack2/TestNGMain.java:3: error: package org.testng.annotations does not exist
  [javadoc] import org.testng.annotations.Test;
  [javadoc]                              ^
  [javadoc] /home/vaav/workspace/AntBasics/src/myPack2/TestNGMain.java:8: error: cannot find symbol
  [javadoc]     @Test
  [javadoc]      ^
  [javadoc]   symbol:   class Test
  [javadoc]   location: class TestNGMain
  [javadoc] Standard Doclet version 1.8.0_45
  [javadoc] Building tree for all the packages and classes...
  [javadoc] javadoc: warning - Class Test not found.
  [javadoc] javadoc: warning - Class Test not found.
  [javadoc] javadoc: warning - Class Test not found.
  [javadoc] javadoc: warning - Class Test not found.
  [javadoc] javadoc: warning - Class Test not found.
  [javadoc] Building index for all the packages and classes...
  [javadoc] Building index for all classes...
  [javadoc] Generating /home/vaav/workspace/AntBasics/docs/help-doc.html...
  [javadoc] 8 warnings
  [javadoc] javadoc: warning - Class Test not found.
runTest:
    [mkdir] Created dir: /home/vaav/workspace/AntBasics/test-output
   [testng] Error: A JNI error has occurred, please check your installation and try again
   [testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
   [testng]     at java.lang.Class.getDeclaredMethods0(Native Method)
   [testng]     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
   [testng]     at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
   [testng]     at java.lang.Class.getMethod0(Class.java:3018)
   [testng]     at java.lang.Class.getMethod(Class.java:1784)
   [testng]     at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
   [testng]     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
   [testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
   [testng]     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
   [testng]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
   [testng]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
   [testng]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
   [testng]     ... 7 more

BUILD FAILED
/home/vaav/workspace/AntBasics/build.xml:71: The tests failed.

Total time: 1 second

Below is my java code

package myPack2;
import org.testng.annotations.Test;
import myPack.Test1;
public class TestNGMain {

    @Test
    public void getTest(){
        Test1 test = new Test1();
        System.out.println("Result===========> "+test.division(20, 10));
    }

}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="AntBasics">
    <test name="order">
        <classes>
            <class name="myPack2.TestNGMain" />
        </classes>
    </test> 
</suite>
1
[testng] Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException Suresh Atta
what should i do now??Anitha
Seems you missed some librariesSuresh Atta
I have put "testng-6.8.5.jar" file in lib library and mentioned that location in classpathAnitha
Do you have multiple copies/different versions of them?Suresh Atta

1 Answers

1
votes

That TestNG JAR does not have JCommander files within it. This is a bug with TestNG ant distribution. You may use the JAR from this ZIP http://testng.org/testng-6.0.1.zip

One may like to refer this thread for more information https://github.com/cbeust/testng/issues/255