JUnit (at http://junit.org/) is a class library for writing unit tests for Java software.

Unit testing is normally practiced by developers, who write tests of individual program units to check them out before releasing them for inclusion in a larger project. It most naturally corresponds to the testing that students might do, since the focus is on testing the features or individual classes or modules. Other kinds of testing include integration testing, which usually focuses on testing interconnected collections of units to make sure they work together when they are integrated into a whole, functional testing, which usually focuses on ensuring an application meets its requirements, and acceptance testing, which is usually performed by a customer to assess whether a development organization has produced an acceptable (custom) product.

JUnit allows one to write software tests in the form of code, so the tests can be easily executed, and can be re-executed as often as you like with near-zero effort. Phrasing tests in the form of code is more useful than running tests by hand, since it makes the process of executing and managing tests very easy to include in automated build processes, and eliminates human error in conducting these actions.

Basic terms:

There are currently three common unit test frameworks in use for Java: JUnit 3, JUnit 4, and TestNG. This page is going to focus on JUnit 3, although JUnit 4 techniques can also be used.

JUnit 3 is the slightly older version of JUnit but the most popular version in many classrooms. For the education market, many introductory textbooks are still using 3.x, so this tutorial will use 3.x examples. Code written for JUnit 3.x will run under JUnit 4, as the newer version includes support for the older tests. There is a separate, official Getting Started page for JUnit v3.8.

JUnit 4 makes use of Java Annotations to simplify the specification of unit tests. The annotations take the place of several conventions that were used in JUnit 3.x testing. JUnit 4 will still execute test cases and suites written using the JUnit 3 conventions. The Getting Started page for JUnit 4 has lots of information on how to use it. You can also use JUnit 4-style annotations and features with your tests when applying the concepts in this tutorial, although the details will be slightly different than the examples shown.

Testing: The Next Generation (TestNG) is a newer framework inspired by JUnit that addresses some of the design limitations of JUnit. We will not be covering TestNG in this tutorial, and many of the features shown in this tutorial will not work with TestNG. IF you use TestNG, you'll have to use somewhat different techniques, so it is not the ideal place for beginners to start. The website for TestNG has documentation and examples on how to use it. We will not cover TestNG but you should be aware of its availability and possible future popularity.