Unit testing for Java and .Net made simple and free

JUnit and NUnit  handle the dirty work of creating and executing unit tests

When taking a test-first approach to programming (see Test before you leap), developers use unit tests to verify that that their code will function as expected. JUnit and NUnit are open source unit testing frameworks; the former for Java applications, the latter for .Net programs. JUnit is a descendant of SUnit, a Smalltalk creation and great-grandaddy of all xUnit frameworks. NUnit is, in turn, derived from JUnit; the current version is the result of a rewrite that gave NUnit .Net capabilities that are impossible in Java.

It's a stretch to say that JUnit and NUnit automate unit testing, but they do provide sufficient support code that vastly simplifies creating and executing unit tests. In the jargon often used to describe higher-level development tools, JUnit and NUnit handle the dirty work, providing a kind of test container, run-time environment, and results reporting structure, so you can focus on the business logic of your tests.

In the case of JUnit, you derive objects from the TestCase class. All methods of the form Testxxx() in TestCase objects are considered test methods, and when the JUnit-enabled application is executed, the framework uses reflection to locate and execute those methods. TestCase objects can be gathered into TestSuite objects, and the framework will recurse into the suite to execute all tests within all the TestCases.

Unit tests in JUnit should be brief, pass/fail affairs. Members of the framework's library of Assertxxx() methods accept as input the result of a test execution and the expected value. Place the proper Assertxxx() method after a specific test method, and the framework scores accordingly. Consequently, test methods are extremely terse, efficient, and readable.

JUnit has spawned a couple of interesting elaborations. JUnitPerf provides test decorators, which act somewhat like wrappers, that add performance testing. For example, run a TestCase that has been decorated with TimedTest, and the test fails if it doesn't execute within a specified time. Another offspring, JUnitEE, moves test capabilities into servlets; TestCases can be created that exercise servlets, EJBs, or conceivably just about anything that runs in a Java application server.

NUnit is written in C#, but, thanks to the .Net architecture, can be unleashed on any .Net language, including C#, VB.Net, Managed C++, and J#. Although NUnit is very similar to JUnit, the latest release uses .Net's capability of associating attributes to classes and methods, a feature sadly lacking in Java. Consequently, where JUnit requires inheritance to identify TestCase classes and naming conventions to identify specific test methods, NUnit uses attributes. A TestCase class (referred to as a test fixture) is identified by the [TestFixture] attribute, and a test method has the [Test] attribute.

But NUnit goes beyond the use of attributes to ease inheritance and naming restrictions. For example, the [ExpectedException()] attribute associated with a test method informs the framework that the test is expected to throw a specified exception that is identified in the attribute. If the exception isn't thrown, the test fails. Another attribute lets you temporarily turn off a specific test that might be causing problems, which is far easier than commenting out the test method, then uncommenting it when it's fixed.

JUnit and NUnit are not the final word in unit testing frameworks. A plethora of worthy "xUnit" testing tools can be found at XProgramming.com.

Copyright © 2003 IDG Communications, Inc.

How to choose a low-code development platform