6

I wanted to invoke testng programmatically. Not eclipse plug-in.

I have associated "testng-6.8.21.jar" and running through eclipse and i ran below code:

import org.testng.TestNG; public class SampCls { public static void main(String[] args) { TestNG test=new TestNG(); } } 

Getting below exception. How can i overcome this exception.

Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException at SampCls.main(SampCls.java:12) Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more 

4 Answers 4

18

If you use a Maven project, you need add this dependancy:

<dependency> <groupId>com.beust</groupId> <artifactId>jcommander</artifactId> <version>1.48</version> </dependency> 

the class com/beust/jcommander/ParameterException is inside

If you use a project without Maven you need add this jar file at your classpath:

jcommander-1.48.jar 

You can download this jar file on central.maven.org -> jcommander-1.48.jar

Sign up to request clarification or add additional context in comments.

6 Comments

As i mentioned, i am not using any maven anything. I just downloaded this jar, launched eclipse, referenced this jar in build path, and wrote the above 3 lines of code. When i run @Test public void hello(){Syso("hello")} it is working fine. But when i invoke testng programatically, it is throwing that exception.
import org.testng.TestNG; public class SampCls { public static void main(String[] args) { TestNG test = new TestNG(); } } is OK, can you give all code please?
Some how now my test is not invoked, Can you help me? import org.testng.TestNG; public class SampCls { public static void main(String[] args) throws ClassNotFoundException { TestNG test=new TestNG(); Class cls=Class.forName("TestSuite.TestCases.AddContactHappyPath").getClass(); test.setTestClasses(new Class[] {cls}); test.run(); } } Following is my test. what i am missing: package TestSuite.TestCases; import org.testng.annotations.*; public class AddContactHappyPath { @Test() public void AddContactHappyPathTest() { System.out.println("hello world"); } }
Output in console is: [TestNG] Running: Command line suite =============================================== Command line suite Total tests run: 0, Failures: 0, Skips: 0 ===============================================
@sgrillon- thanks, adding jcommander-1.48.jar worked for me :)
|
3

Change:

Class cls = Class.forName("TestSuite.TestCases.AddContactHappyPath").getClass(); test.setTestClasses(new Class[] { cls }); 

By:

 test.setTestClasses(new Class[] { AddContactHappyPath.class }); 

All code is

import org.testng.TestNG; import com.xxx.test.others.AddContactHappyPath; public class SampCls { public static void main(String[] args) throws ClassNotFoundException { TestNG test = new TestNG(); test.setTestClasses(new Class[] { AddContactHappyPath.class }); test.run(); } } 

TestNG code is:

import org.testng.annotations.*; public class AddContactHappyPath { @Test() public void AddContactHappyPathTest() { System.out.println("hello world"); } } 

Console result:

[TestNG] Running: Command line suite hello world =============================================== Command line suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== 

2 Comments

As the class exists in a package, i suppose we need to provide the complete path like Class.forName("TestSuite.TestCases.AddContactHappyPath") and the below worked for me: test.setTestClasses(new Class[] { Class.forName("TestSuite.TestCases.AddContactHappyPath") });
I update the code with the testNG in a other package. I add import com.xxx.test.others.AddContactHappyPath;
0

As @sgrillon correctly pointed out, you need the correct Maven dependency, but also the shade plugin (https://maven.apache.org/plugins/maven-shade-plugin) to package a Uber-jar including all Maven dependencies for easy execution. This is what should be included in your pom.xml:

... <dependencies> <dependency> <groupId>com.beust</groupId> <artifactId>jcommander</artifactId> <version>1.48</version> </dependency> </dependencies> ... <plugins> ... <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>runnable</shadedClassifierName> </configuration> </execution> </executions> </plugin> </plugins> 

After you build the Maven package, you'll get your regular my-app-1.0-SNAPSHOT.jar file and also a my-app-1.0-SNAPSHOT-runnable.jar. This is what you should run, with the command:

$ java -jar my-app-1.0-SNAPSHOT-runnable.jar 

You can verify with this command:

$ jar tvf my-app-1.0-SNAPSHOT-runnable.jar 

that the shaded jar contains the JCommander classes (and those of all the other Maven dependencies), while the regular one doesn't.

Comments

0

Solution for me was to install TestNg. I referred this link here. As I was using latest version of eclipse, Eclipse MarketPlace option didn't work for me, if it works for you then great. Else goto Help -> Install new software and work with http://dl.bintray.com/testng-team/testng-eclipse-release/ After installation refer 1st link I mentioned to continue. All the best !!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.