Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

JUnit and Its Extensions Make Unit Testing Your Java Code Much Easier

In an age when any professional Java developer has to know how to properly unit test the code he or she writes, JUnit and its extensions can make writing your tests easier and more effective.


advertisement
erhaps the most popular Java open-source tool available today is JUnit. Originally developed by Kent Beck and Eric Gamma for use in Extreme Programming, JUnit has rapidly become a standard part of Java developers' professional lives. Martin Fowler wrote: "Never in the field of software engineering has so much been owed by so many to so few lines of code." And he was absolutely right.

This article focuses on some of the issues you'll face once you've learned the basic operational facts of JUnit and start writing real tests. It also introduces a few extensions to JUnit that will make your life a lot easier.

Author's Note: If you haven't already, you'll need to download the latest version of JUnit and at least run through the excellent FAQ that accompanies it. It will show you how to setup JUnit and write basic tests. At the time I wrote this article, the latest version of JUnit was 3.8.1. That's the version I use throughout.



JUnit-Addons
The first JUnit extension framework that we'll examine is called JUnit-Addons, which at the time of writing was at version 3.7.1. Among its other features, JUnit-Addons solves two awkward problems for Java developers aiming to test their code:

  1. How to check the contents of two arrays for equality
  2. How to access private members

The ability to do both of these things is a very common requirement, but they aren't mandatory for all projects so they were relegated to an extension. Let's examine each problem and see how JUnit-Addons can solve it.

How Do I Check the Contents of Two Arrays for Equality?
In JUnit, an assertion performs an individual test (not to be confused with the new assertion keyword introduced in JDK1.4). The most common assertions are perhaps equality assertions. These compare two values—the test passes if they're equal and fails if they're not. The idea is to have the assertion compare test data with the data the class being tested generates.

All JUnit assertions take either primitives or objects as their arguments. The lack of array-type parameters means that if two arrays are compared they'll be treated as references, and only the reference values will be compared. This is rarely the behavior that a developer is looking for as it usually causes a test to fail!

JUnit-Addons solves this issue by providing a suite of static methods on its ArrayAssert class, which take arrays of primitives and objects as parameters. When testing two arrays, it iterates through them and compares their contents (as you would expect).

The following simple example illustrates how you can write such a test:

public void testArrays() { String[] testData = {"one", "two", "three", "four", "five"}; Fragment fragment = new Fragment(); ArrayAssert.assertEqualArrays(testData, fragment.getNumbers()); }

To use a test like this you'd replace Fragment with the class being tested. Of course, the method getNumbers() must return an array. The bold text indicates where JUnit-Addons was used.

At this time, multidimensional arrays still are a problem. Rather than treat these as arrays of arrays, JUnit-Addons simply compares references. This is a problem that's currently being worked on by the JUnit community and hopefully an updated version of the library that provides this feature will be available soon.

How Do I Access Private Members?
The need to test private members of an object most commonly arises when following the Test-Driven-Development (TDD) process for writing code. In this process, the developer writes a simple test, implements code to make it pass, and refactors before moving on to the next simple test.

One of the consequences of TDD is that methods get built up slowly in a number of very small iterations. This applies to private methods as well as public methods, and so you need a way to incrementally test private methods.

If your approach is to develop code before writing the tests for it, then you can infer the correct behavior of private methods through the behavior of public methods and attributes. However, even in this process, having the ability to test a private method or attribute is sometimes useful.

Not many programmers know that reflection can, in certain cases, completely bypass encapsulation and provide access to an object's otherwise encapsulated members. JUnit-Addons uses this fact to provide a few simple methods for testing methods and attributes on an object or class (static) level.

The following code fragment illustrates how to use these features to write a test that accesses a private vector at class level (static):

public void testVectorSize() throws NoSuchFieldException { Thing dummy1 = Factory.createThing(); Thing dummy2 = Factory.createThing(); Vector things = (Vector) PrivateAccessor.getField(Factory.class, "things"); assertEquals(2, things.size()); }

This is part of a suite of tests on a factory class (according to Erich Gamma, et al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1994.). The Factory class is responsible for creating instances of Thing. It also maintains a vector (called things) of references to each Thing it instantiates. This test simply confirms that the vector contains the correct number of elements. The bold text shows where you use JUnit-Addons to access the private state of the factory class. The PrivateAccessor class provides four static methods for accessing private members: two each for objects and classes (static), providing facilities to test both methods and—as shown above—attributes/fields.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap