JUnit tests software code units by making assertions that satisfy the expected requirements, but these assertions are limited to basic operations. IBM Software engineer Tony Morris fills this gap by introducing the JUnit assertion extension (assertion Extensions for JUnit), and the JUnit assertion extension provides a complete set of complex assertions executed in the JUnit framework. Follow the author's demo to learn how to use this new package from Alphaworks to improve the reliability and robustness of your Java software.
The popular JUnit Automated Unit Test framework provides a way to test the software code unit by making assertions that meet the expected requirements. However, these assertions are limited to basic operations, such as "assertion two variables equal" and "assertion reference variable not NULL." Basic JUnit assertions are useful, but they do not provide the many complex assertions required in a realistic software unit test scenario.
The JUnit assertion extension (assertion Extensions for JUNIT,JUNITX) is an expansion pack for the JUnit framework that can be downloaded from alphaworks (see Resources). It provides a necessary implementation of many common, complex assertions. Instead of writing complex JUnit test cases for an assertion, you can call a Junitx method, make assertions from the same context-and do not need additional settings. JUNITX also claims that its functionality is the same as the documentation description and includes its own JUnit self test suite. This again guarantees that JUNITX will make assertions based on the JUNITX document, and that if a unit test fails, it is possible to know that the software code unit failed, rather than the JUNITX test implementation reporting a false failure.
A typical scenario for JUNITX will include the Equals (Object) method of the Java.lang.Object class and the contract set by the Hashcode () method. In the developed class, these contracts are usually required. Using JUnit to make compliance assertions requires the development of complex unit test cases, which can be easily faulted. In contrast, assertions with JUNITX are very simple, as simple as creating a factory implementation that returns an instance of a class and calling Junitx's Assertequalscontract and asserthashcodecontract from a unit test case.
JUNITX start
Effective use of JUNITX only requires a shortest learning curve, and if you know how to use the JUnit automated test framework directly, you will find it easy to use the JUNITX expansion pack. Follow these steps to start:
If you have not yet set up the environment to run your JUnit test cases, set it up first. You can see the steps described in more detail in "Automating the build and test process" (see Resources).
Download the JUNITX package (see Resources) to extract the Junitx document to the selected directory.
Let the class loader that executes the JUnit test be able to find the Lib/junitx.jar file.
You can now call the methods on the JUnitx.framework.Assert class to assert the functionality in a way that is similar to the way you use the JUnit.framework.Assert class in a typical JUnit test environment. JUNITX's online API documentation (see Resources) provides a detailed description of the method calls that can be used on the JUnitx.framework.Assert class.
Use case scenarios
Suppose you now want to implement a class that represents a person. The person class requires three attributes: Title, first name, and surname (surname). The Title property is a value in a finite set of possible values for MR, MS, and MRS, so you are ready to implement a Title class with the Typesafe enumeration (type-safe enumeration) design pattern. The UML diagram in Figure 1 shows the current state of these requirements.
Figure 1. UML Diagram of requirements
Listing 1 shows the source code for these requirements: