JUnit and TestNG

JUnit and TestNG

In the IDE, the tools provided to help you with JUnit and TestNG are identical, so you will learn about them in this same section. Both are applicable to unit testing, that is, one or the other should be used when you are testing the individual methods in your applications.

In both cases, the starting point for working with these frameworks is to select a project in the Projects window and then go to the New File window (Ctrl+N). In the Unit Tests category, you’ll find templates for getting started with JUnit and TestNG, as shown in Figure 7-1 .

Figure 7-1. File templates for unit tests

Alternatively, right-click a Java file in the Projects window and choose Tools ➤ Create/Update Tests (Ctrl+Shift+U). The Create Tests window opens for the currently selected file, enabling you to choose either

JUnit or TestNG. You can also use all the other settings found in the wizards in the Unit Tests category shown in Figure 7-1 .

As shown in Figure 7-2 , the wizards in the Unit Tests category are detailed and help generate exactly the kind of tests that you need. For detailed information on JUnit and TestNG, see their online documentation, at junit.org and testng.org, respectively.

Figure 7-2. New Test for Existing Class wizard

For JUnit, when you click Finish in Figure 7-2 , you are prompted to select whether the generated tests should be based on JUnit 3 or JUnit 4, as shown in Figure 7-3 . With JUnit 4, your tests can use Java features introduced in JDK 5, such as generics and annotations. In order to use JUnit 4, the version of the JDK used by your application should at least be JDK 5.

Figure 7-3. Select JUnit Version window When you complete any of the Unit Testing wizards, the IDE creates a starting point that complies to the

selected unit testing framework. In addition, the related JARs are added to the classpath of your application and, in the case of Maven-based applications, are registered in the POM file, while also being shown in the Test Dependencies node in the Projects window. Their interrelated dependencies can be visualized in the Graph view of the Maven POM, as shown in Figure 7-4 for TestNG.

Figure 7-4. Test dependencies shown in the Projects window and Graph view

A useful related hint is that you can easily find a Java file’s unit test file. Open the Java file in the editor, such as by double-clicking on it, and then press Ctrl+Alt+T. If a related test file exists, it will be opened. If the file does not exist, you will be prompted to create it.

Once you have written your tests, you can run them. To run all the tests in a project, right-click the project and choose Test (Alt+F6). To run the test file for a specific Java file, right-click the Java file and choose Test (Ctrl+F6). To run a specific test method, open the test file in the Source Editor, right-click in the signature of the method you want to test, and choose Run Focused Test Method.

The Test Results window (Alt+Shift+R) opens to show the results of the tests, as shown in Figure 7-5 .

Figure 7-5. Test Results window In the Test Results window, double-click an entry to jump to the related source code that defines the test

method. The buttons along the left of the Test Results window let you rerun test methods and filter out items from the test Results window.

More features can be incorporated into your unit tests. For example, when you use the JUnitReport Ant task or the Maven surefire-report plugin, reports can automatically be generated in various formats during the testing process.