Eclipse - Install TestNG plugin

By xngo on June 11, 2019

Overview

TestNG is a testing framework that is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc...

Install TestNG plugin

  1. Navigate to Help > Eclipse Marketplace....
  2. Search TestNG and follow the instructions.
  3. You also need to add Jcommander JAR file to your library class path. Otherwise, when you run TestNG, it will show the following error message:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

Here is a video showing how to install TestNG plugin in Eclipse.

Install TestNG plugin in Eclipse

Sample codes

import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertEquals;
 
import org.testng.annotations.Test;
 
public class TestngHelloWorld {
    @Test(description = "AssertTrue: Test positive.")
    public void testAssertTruePositive() {
        assertTrue(true, "Positive assertTrue().");
    }
 
    @Test(description = "AssertFalse: Test negative.")
    public void testAssertFalseNegative() {
        assertFalse(true, "This will fail.");
    }
 
    @Test(description = "AssertEquals: Test negative.")
    public void testAssertEqualsNegative() {
        String actual = "Some very long string.";
        String expected = "some string";
        String message = "Error message to display if failed.";
        assertEquals(actual, expected, message);
    }
}

Output results of the code above

Github

  • https://github.com/xuanngo2001/java-testng/blob/master/src/net/openwritings/testng/TestngHelloWorld.java

Reference

  • https://testng.org/doc/documentation-main.html

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.