0
votes

Test (present in project-tests.jar):

package sample;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.Test;
import utils.Base;

public class SampleTest {
    @Test
    public void Test() {
        log.info("Sample Test called successfully.");

        log.info("Invoking Base Method");
        Base b = new Base();
        b.testMethod();
    }
}

Util (present in project.jar):

package utils;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map.Entry;

public class Base {
    public void testMethod() {
        log.info("Test method in utils.Base class called.");
    }
}

Assume that log is already created and usable in both the classes.

Build.xml:

<project default="TestNGSimpleTest">

    <path id="main_cp">
        <pathelement location="dependency/testng-6.9.10.jar"/>
        <pathelement location="dependency"/>
        <pathelement location="."/>
    </path>

    <taskdef name="testng" classpathref="main_cp"
             classname="org.testng.TestNGAntTask" />

    <target name="test">
        <testng classpathref="main_cp" >
            <classfileset dir="." includes="sample/*.class"/>
        </testng>
    </target>

</project>

Target directory structure:

./dependency/{all-dependencies}.jar
./project.jar
./project-tests.jar
./build.xml

{all-dependencies} include:

bsh-1.3.0.jar
bsh-2.0b4.jar
commons-beanutils-1.7.0.jar
commons-beanutils-core-1.8.0.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-1.8.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
jcommander-1.48.jar
testng-6.9.10.jar

When I run the SampleTest from IDE it runs fine, but when I try to run it using ant (ant -v test), I hit the following error:

[testng] The ' characters around the executable and arguments are
[testng] not part of the command.
[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
[testng] The tests failed.

I think the issue is with build.xml (since I could run it in IDE), but not able to figure out why this error (java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException) and how to fix it. Am I specifying the classfileset incorrectly? How do I make this run?

Went through other threads that suggested that the testng jar could be corrupt, no jcommander jar, invalid classpath - tried them but did not help.

1

1 Answers

1
votes

To answer directly to the question, it seems that you have to include

<pathelement location="dependency/jcommander-1.48.jar"/>

in main_cp.

This question duplicates an older Suddenly can't run TestNG tests from ant ([testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException) whose answers seem to be all outdated.

Indeed, it seems that since long, jcommander is not included with either the sonatype snapshots nor the jar that you generate with the github/graddle distrib.

Hence, you have to add yourself the jcommander jarfile in the path passed to the testng ant tag.

Your version of jcommander seems old and the new recommended way to declare the testng task is:

<taskdef resource="testngtasks" classpath="testng.jar" />