DocsTestNGTestDebug

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.

Contents


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 WordLibrary.java 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
@BeforeMethod
    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.
    @org.testng.annotations.Test
    public void testIsCorrect() {
        for (int i = 0; i < wordLibrary.getSize(); i++) {
            String clearWord = wordLibrary.getWord(i);
            String scrambledWord = wordLibrary.getScrambledWord(i);
            System.out.println(scrambledWord);
            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();
        Arrays.sort(clearArray);
        Arrays.sort(scrambledArray);
        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 WordLibrary.java 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