There are many JUnit 3 tests available. The emphasis on unit testing and test-driven development has resulted in developers building a significant corpus of JUnit tests using the JUnit 3 test framework. The migration of these unit tests to JUnit 4, however, has been rather slow. Part of the reason for this is the slow transition from JDK 1.4.x to Java 5 (or later).

Another reason is the lack of enthusiasm (though it’s hard to generate) for learning new features and moving to a new version of a framework that serves as well as it does today.

However, the time is approaching when the features offered by JDK 5, 6, and soon 7 will be convincing enough to make the transition to a later version of Java. When the day comes to upgrade to Java, it will come with the option to upgrade to JUnit 4.

So, what happens to your JUnit 3 unit tests if you want to upgrade to JUnit 4? Do you have to change all of your tests to keep them running? (No.) How many modifications are needed? (Some, but not many.) Are there any benefits to upgrading to JUnit 4 other than avoiding versioning? (Very definitely yes.)

PREREQUISITES

Before we get into the details of the test migration, let’s take a look at what you need to work with. On the Java side, the only prerequisite is Java 5 or later. That’s pretty simple. As always, the Java JDK is available for download from the Sun Java website.

Why do you need 1.5 or later? Primarily for annotations. JUnit 4 also uses generics and related auto-wrapping to good advantage, but the most notable new aspects of JUnit 4 come from the use of annotations. With the release of the JDK 1.5 in the fall of 2004, the creators of JUnit changed JUnit to an annotation-based framework. Today, JUnit is a great example of using the power of annotations, but the implication of annotation-based technology is that the developer uses JDK 1.5 or later.

On the JUnit side, you have to get the latest version of JUnit; currently it is 4.4. This can be obtained from the JUnit website. After installing JUnit 4, make sure that the JUnit 4 jar (current jar junit-4.4.jar) replaces the JUnit 3.x jar in all class paths used during testing.

And that’s it for the prerequisites, except for the not-so-small assumption that you know what a JUnit test is. This article is about moving from JUnit 3.x to 4.x. If you’re new to JUnit and looking for an introduction to the JUnit testing infrastructure, I recommend checking out the JUnit Cookbook, the introductory article for many of us. The book Pragmatic Unit Testing by Andrew Hunt and David Thomas also provides an introduction (and more) to both unit testing and JUnit.

THE GOOD NEWS ABOUT JUNIT 4

I could get it in black and white. I consider almost everything about JUnit 4 to be good news. And the first good news is that all 3.x unit tests run unchanged with the JUnit 4 jar.

JUnit 3 uses junit.framework.* packages, and JUnit 4 uses the org.junit.* package hierarchy. By using different package structures, the JUnit staff has ensured that JUnit 3 tests run smoothly. JUnit 4 ships with both packages, so you can easily do your basic first migration step, i.e. use the new JUnit 4 jar instead of your current JUnit 3 jar. After that, all of your JUnit tests continue to run as before, painlessly.

This is great news because it allows developers to migrate their tests gradually and gracefully, rather than imposing the big bang, change-everything-at-once approach that is so often required when moving to a newer release of legacy software.

The transition to JUnit 4 consists of two different stages. The first step is to convert your existing JUnit 3 test cases to their JUnit 4 counterparts. In the second phase, you take advantage of the new features of JUnit 4 when refactoring existing tests and writing new ones.

CONVERSION: CHANGING THE PAST

Because the structure of the JUnit 4 package is different, you will need to change the junit.framework.* import to an org.junit.* import. The org.junit packages are similar in structure and are well documented in the javadocs . New annotations have the form org.junit. For example, @Test annotation is imported using import org.junit.Test;.

New in JUnit 4 is the need to import static Assertion methods. In JUnit 3, the test class would extend the TestCase class, which, in turn, would extend the Assert class and make assertion methods available. JUnit 4 tests no longer extend TestCase, so you need to import assertions specifically. To get assertions, use another JDK 1.5 feature, static import.

From a conversion perspective, having all the JUnit 3 assertions in JUnit 4 means that you don’t need to convert any of the assertions used in the tests that are being migrated.