This article contains the following sections:
- Why do we need JUnit?
- JUnit Basics
- Install JUnit
- Write a Test Case
- Write a Test Suite
- Run the Tests
- Resources
Why do we need JUnit?
JUnit actually helps you write code faster while increasing code quality. Once you start using JUnit you'll begin to notice a powerful synergy emerging between coding and testing, ultimately leading to a development style of only writing new code when a test is failing.
Here are just a few reasons to use JUnit:
- JUnit tests allow you to write code faster while increasing quality.
- JUnit is elegantly simple.
- JUnit tests check their own results and provide immediate feedback.
- JUnit tests can be composed into a hierarchy of test suites.
- Writing JUnit tests is inexpensive.
- JUnit tests increase the stability of software.
- JUnit tests are developer tests.
- JUnit tests are written in Java.
- JUnit is free!
Any class having test methods should subclass TestCase class. It can contain any number of public testXXX methods. Test class can use the setUp() and tearDown() methods to initialize and release any common objects under test. These two methods will be called before and after each test method to ensure there can be no side effects among test runs.
TestCase instances can be composed into TestSuite hierarchies that automatically invoke all the testXXX() methods defined in each TestCase instance. A TestSuite is a composite of other tests, either TestCase instances or other TestSuite instances. The composite behavior exhibited by the TestSuite allows you to assemble test suites of test suites of tests, to an arbitrary depth, and run all the tests automatically and uniformly to yield a single pass or fail status.
Install JUnit
- First, download the latest version of JUnit, referred to below as junit.zip.
- Then install JUnit on your platform of choice:
Windows
To install JUnit on Windows, follow these steps: - Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.
- Add JUnit to the classpath:
set CLASSPATH=%JUNIT_HOME%\junit.jar
Unix (bash)
To install JUnit on Unix, follow these steps: - Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.
- Add JUnit to the classpath:
export CLASSPATH=$JUNIT_HOME/junit.jar
- Test the installation by using either the textual or graphical test runner to run the sample tests distributed with JUnit.
Note: The sample tests are not contained in the junit.jar, but in the installation directory directly. Therefore, make sure that the JUnit installation directory is in the CLASSPATH.
To use the textual test runner, type:
java junit.textui.TestRunner junit.samples.AllTests
To use the graphical test runner, type:
java junit.swingui.TestRunner junit.samples.AllTests
All the tests should pass with an "OK" (textual runner) or a green bar (graphical runner). If the tests don't pass, verify that junit.jar is in the CLASSPATH.
Write a Test Case
Here we'll write a test case to exercise a single software component.
To write a test case, follow these steps:
- Define a subclass of TestCase.
- Override the setUp() method to initialize object(s) under test.
- Optionally override the tearDown() method to release object(s) under test.
- Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.
The following is an example test case:
Write a Test Suite
Here we will write a test suite that includes several test cases. The test suite will allow us to run all of its test cases.
To write a test suite, follow these steps:
- Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests.
- Optionally define a main() method that runs the TestSuite in batch mode.
The following is an example test suite:
Run the Tests
To run our test case using the textual user interface, use:
java junit.textui.TestRunner CalculationTestCase
The textual user interface displays "OK" if all the tests passed and failure messages if any of the tests failed.
Resources




No comments:
Post a Comment