Simple Testing and Debugging of TestNG Unit Test

This tutorial demonstrates how to create TestNG unit tests for a Java application. The tutorial demonstrates how to create, run and debug the tests.


Requirements for This tutorial

You need the following software:

  • NetBeans IDE 7.2

This tutorial

Create Java Application

To create the Java application:

  1. Choose File > New Project (Ctrl-Shift-N). Select Maven Java Application from the Maven category and click Next.
  2. Type mavenanagram for the project name and set the project location. Click Finish.
  3. Create the anagram game files.
  4. Delete the default generated test class and the dependency on JUnit
  5. Right-click and choose Tools > Create Tests
  6. Select TestNG as the Framework in the Create Tests dialog box. Click OK.
  7. Modify the setup method and add the wordLibrary field to the test class
    public void setUp() {
        wordLibrary = WordLibrary.getDefault();
    private WordLibrary wordLibrary;
  1. Delete the generated test skeletons
  2. Add the following test method to the test class.
    public void testIsCorrect() {
        for (int i = 0; i < wordLibrary.getSize(); i++) {
            String clearWord = wordLibrary.getWord(i);
            String scrambledWord = wordLibrary.getScrambledWord(i);
            Assert.assertEquals(isAnagram(clearWord, scrambledWord), true);
  1. Add the following
   private boolean isAnagram(String clearWord, String scrambledWord) {
        char[] clearArray = clearWord.toCharArray();
        char[] scrambledArray = scrambledWord.toCharArray();
        return Arrays.equals(clearArray, scrambledArray);
  1. Right-click project and choose Test.

The IDE will build the project and run the tests.

Notes. Seems that right-clicking and choosing Test File does not work.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo