How to install TestNG in NetBeans?

First I added the dependency and repository to my project's maven pom.xml file:

<repositories>
    <repository>
        <id>jcenter</id>
        <name>bintray</name>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

 <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.10</version>
    <scope>test</scope>
</dependency>

and did a NetBeans "Clean and Build". It downloaded TestNG stuff but project code still showed it couldn't find the classes it needed. I can see that C:\Users\Greg.m2\repository\org\testng\testng\6.10\testng-6.10.jar exists. If I open that jar file I can see that org\testng\Assert.class exists. Why can't NetBeans find it?

Since that failed, I followed instructions for setting up TestNG Environment here: https://www.tutorialspoint.com/testng/testng_environment.htm

My TESTING_HOME environment variable is set to

C:\Users\Greg\Projects\TestNG

My CLASSPATH variable is set to

%CLASSPATH%;%TESTING_HOME%\testng-6.10.jar

Yes, that jar file exists there.

I open up a new command shell and navigate to

C:\Users\Greg\Projects\Enventive\en360\src

where there exists testng.xml

I then issue the following command per the TestNG author's doc

java org.testng.TestNG testng.xml

but get

Error: Could not find or load main class org.testng.TestNG

In NetBeans 8.2, I can create a TestNG class using

File>New File...>Unit Tests>TestNG Test Case

It produces test code but all the annotations have errors due to the error on the import line

import static org.testng.Assert.*;

which states

package org.testng does not exist

If I attempt to install the TestNG Plugin in NetBeans by downloading Maven TestNG Support and then trying to install it using

Tools>Plugins>Downloaded>[navigate to downloaded org-netbeans-modules-testng-maven.nbm]>Install

It fails to install with this error:

Some plugins require plugin Maven Embedder to be installed. The plugin Maven Embedder is requested in version >= 2.54 but only 2.52.1 was found. The following plugin is affected:       Maven TestNG Support

A search for "Maven Embedder" in "Available Plugins" tab, or on the web finds nothing.

1 answer

  • answered 2018-10-11 22:48 user3217883

    The problem was that the scope tag needs to be "compile", not "test". The author's doc specifically says to use "test". Took me all day to find the solution here :-(

    Maven Compilation error [package org.testng.annotations does not exist]

    While that fixed it for NetBeans, it did not for the command line. I managed to get that running by specifying the actual jar file in the -cp (classpath) parameter, like this:

    java -cp C:\Users\Greg\Projects\MyCompany\MyProject\src;c:\users\Greg\Projects\TestNG\testng-6.10.jar org.testng.TestNG testng.xml
    

    where the first path is the location of the testng.xml and the second path is to the jar file. Once again the author's doc is faulty in that it only shows a path to the folder containing the testng.xml.

    Although this does find both the TestNG class and the testng.xml, an exception occurs:

    Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException

    That seems strange because my testng.xml does not contain any parameters. (Maybe it should?) Anyway, looking in the jar file, indeed, there is a com/beust but no jcommander folder.

    I downloaded the latest testng-6.14.3.jar and there is no com/beust/jcommander in that one either.

    Here is my testng.xml. Can anyone can see a problem?

    <?xml version = "1.0" encoding = "UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="E360 Test Site" verbose="10"> 
      <test name="Test Analysis Results" preserve-order="true">
        <classes>
          <class name="test.java.core.EnProjectIT">
            <methods>
              <include name="testCreateTables" />
            </methods>
          </class>
        </classes>
      </test>
    </suite>
    

    To overcome this problem I added the following dependency to my pom.xml:

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

    Then added it to the classpath on the command line like this: java -cp C:\Users\Greg\Projects\Enventive\en360\src;c:\users\Greg\Projects\TestNG\testng-6.14.3.jar;c:\users\Greg\Projects\TestNG\jcommander-1.72.jar org.testng.TestNG testng.xml

    Now I get the following error:

    Cannot find class in classpath: test.java.core.EnProjectIT

    Ok, so I need to add the compiled class location, not the java code location to the classpath. Now it looks like this:

    java -cp C:\Users\Greg\Projects\MyCompany\MyProject\src;C:\Users\Greg\Projects\MyCompany\MyProject\target\classes;c:\users\Greg\Projects\TestNG\testng-6.14.3.jar;c:\users\Greg\Projects\TestNG\jcommander-1.72.jar org.testng.TestNG testng.xml

    New error is:

    Cannot instantiate class test.java.core.EnProjectIT Caused by: java.lang.NoClassDefFoundError: org/kohsuke/args4j/CmdLineException

    Seems I am just kicking the can down the road. Maybe I need to put my whole .m2 repo in the classpath?

    Ok, I found a clever way to fix all these dependency issues. Since NetBeans was able to build the project ok, it must have a way of gathering all the jar files into its classpath. Sure enough, I found all of them in one location: C:\Users\Greg\Projects\MyCompany\MyProject\target\ProductSnapshot\WEB-INF\lib*

    I added that to the -cp which now looks like this:

    java -cp C:\Users\Greg\Projects\MyCompany\MyProject\src;C:\Users\Greg\Projects\MyCompany\MyProject\target\classes;C:\Users\Greg\Projects\MyCompany\MyProject\target\ProductSnapshot\WEB-INF\lib\* org.testng.TestNG testng.xml
    

    Well I think I'm very close now. Next error says it can't find a project file. It looks like I need to run the program from a different directory.

    java.io.FileNotFoundException: C:\Users\Greg\Projects\MyCompany\MyProject\src\WEB-INF\EnDatabaseTest.properties (The system cannot find the path specified)

    It's appending WEB-INF\EnDatabaseTest.properties to the startup directory so I will make the startup directory C:\Users\Greg\Projects\MyCompany\MyProduct\WebContent

    I put the testng.xml in that directory, cd'd to that directory, and ran this:

    java -cp C:\Users\Greg\Projects\MyCompany\MyProject\WebContent;C:\Users\Greg\Projects\MyCompany\MyProject\target\classes;C:\Users\Greg\Projects\MyCompany\MyProject\target\En360-1.0.0\WEB-INF\lib\* org.testng.TestNG testng.xml
    

    And it worked!