Problems running testNG from a .jar - eclipse/maven/java/testNG

1,547 views
Skip to first unread message

QA Tester 2014

unread,
Jun 12, 2014, 4:58:44 PM6/12/14
to testng...@googlegroups.com
Hello everyone and thanks for any responses in advance.
I am new to the automation world and am trying to run testNG using Maven.
I am able to run my tests using the conventional way of right-click on testng.xml - run as TestNG suite and everything works as it should.
I would like to compile the project and execute testNG tests by running the jar file. (java -jar mytest-0.0.1-SNAPSHOT.jar) and have the TESTS run.
I can get a hello world to run in the main but past that I cannot get the tests to run.
Any advise would be greatly appreciated. TIA

The set up:

src/main/java has 
com.test.main / main.java
com.test.suiteA / test_case_1.java
.
.
.
(In the root folder)
testng.xml
POM.xml


My POM looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test</groupId>

  <artifactId>test</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  

  

   <dependencies>

   

  <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>compile</scope>

</dependency>

   <dependency>

  <groupId>org.testng</groupId>

  <artifactId>testng</artifactId>

  <version>6.1.1</version>

  <scope>compile</scope>

  </dependency>

    <dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.10-FINAL</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-examples</artifactId>

<version>3.10-FINAL</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-excelant</artifactId>

<version>3.10-FINAL</version>

</dependency>

    <dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.10-FINAL</version>

    </dependency>

    <dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>3.10-FINAL</version>

    </dependency>

    <dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-scratchpad</artifactId>

<version>3.10-FINAL</version>

    </dependency>

  </dependencies>

   <build>

  <plugins>

          <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-jar-plugin</artifactId>

                <version>2.4</version>

                                    <executions>

                    <execution>

                        <goals>

                            <goal>test-jar</goal>

                        </goals>

                    </execution>

                </executions>

                    <configuration>

    <archive>

      <manifest>

      <addClasspath>true</addClasspath>

      <classpathPrefix>lib/</classpathPrefix>

          <mainClass>com/test/main/main</mainClass>

      </manifest>

    </archive>

   </configuration>


            </plugin>

    </plugins>

    </build>

</project>


I would like to have a way to point to and run the testng.xml from the main.java class when executing the .jar file.
Any guidance would be greatly appreciated! 

Thanks again,
QA Tester 2014

Krishnan Mahadevan

unread,
Jun 14, 2014, 1:09:01 AM6/14/14
to testng...@googlegroups.com
Abraham,

What you are asking for would be technically equivalent to writing a miniature version of the TestNG suirefire plugin.

But here's a sample main() method that will let you run ONLY testng xml suites.



public static void main(String[] args) {
    XmlSuite suite = new XmlSuite();
    suite.setName("CommandLine Suite");
    List<String> files = new ArrayList<String>();
    files.addAll(Arrays.asList(args));
    suite.setSuiteFiles(files);
    TestNG tng = new TestNG();
    tng.setXmlSuites(Arrays.asList(new XmlSuite[] {suite}));
    tng.run();
}


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

QA Tester 2014

unread,
Jun 14, 2014, 8:46:44 AM6/14/14
to testng...@googlegroups.com

Thanks for the response Krishnan.
After placing this in the main it still does not work.
I get an error that reads "Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/xml/XmlSuite"

I am not sure how it knows where the testng.xml is.

Krishnan Mahadevan

unread,
Jun 14, 2014, 8:56:33 AM6/14/14
to testng...@googlegroups.com
Looks like you haven't created your jar properly. You would need to do one of the following:
1. Create a uber jar that includes TestNG classes also (or)
2. Include the TestNG jar and resort to using : 
java -cp myjar.jar:testng.jar com.mycompany.MyMainClass

~ Krishnan

iSent. iPhone. iReget.iTypos!
--

QA Tester 2014

unread,
Jun 15, 2014, 12:35:20 AM6/15/14
to testng...@googlegroups.com
Thanks so much for the help Krishnan.
Now I am getting "no main manifest attribute in...jar"

My POM looks like this:

<groupId>com.viacom</groupId>

<artifactId>TestNG2</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>TestNG2</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<build>

<pluginManagement>

<plugins>

<plugin>

<inherited>true</inherited>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.12.2</version>

<configuration>

<suiteXmlFiles>

<suiteXmlFile>testng.xml</suiteXmlFile>

</suiteXmlFiles>

</configuration>

</plugin>

</plugins>

</pluginManagement>

</build>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.10-FINAL</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.10-FINAL</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>3.10-FINAL</version>

</dependency>

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.8.8</version>

</dependency>

</dependencies>

</project>

Thanks again.

Krishnan Mahadevan

unread,
Jun 15, 2014, 12:47:27 AM6/15/14
to testng...@googlegroups.com


~ Krishnan

iSent. iPhone. iReget.iTypos!
Reply all
Reply to author
Forward
0 new messages