JUnit 4 Issues and Alternatives

Description

This note describes issues regarding JUnit 4 backwards compatibility and alternative unit test packages considered for testing of the Proteios 2 project.

JUnit 4 Backwards Compatibility

The new version 4 of JUnit, released 2006-02-16, uses the possibility for annotations in Java 5 to annotate test methods with @Test, instead of starting the method names with "test", as in version 3.8.1 and previous versions. The setUp() and tearDown() methods previously used to create test fixtures, are also replaced by methods annotated with @Before and @After, respectively. More than one method can now be annotated to be executed before and after a test.

The documentation for the new version in junit4.0.zip, e.g. junit4.0/doc/faq/faq.htm, or junit4.0/doc/cookbook/cookbook.htm, do not mention (to the authors knowledge) if it is compatible with test methods written according to the earlier standard. The documents were searched using words like "compatibility". Many web pages on JUnit-related web sites have not been updated to include information on the new version. The article "An early look at JUnit 4" from 2005-09-13 at http://www-128.ibm.com/developerworks/java/library/j-junit4.html states that JUnit 4 supports testing code containing unit tests written according to the earlier standard. However, JUnit 4 will only work with Java 5.

The web site http://www.theserverside.com/news/thread.tss?thread_id=39048 contains a discussion regarding the pros and cons of the new JUnit version. No new essential information on JUnit 4 is given, as the previously mentioned article is used as reference. Most of the criticism is centered on the necessity of Java 5. An alternative unit test package often mentioned is TestNG ("Testing, the Next Generation") at http://testng.org/doc/.

TestNG

The documentation for TestNG has been downloaded and inspected. It includes references to discussions containing criticism of JUnit, that lead to the development of TestNG (list of links in chronological order):

  1. http://www.jroller.com/page/cbeust/20030317
  2. http://jroller.com/page/cbeust/20030425
  3. http://www.beust.com/weblog/archives/000082.html
  4. http://www.beust.com/weblog/archives/000173.html

TestNG uses annotations in Java 5 to mark tests according to a scheme similar to the one used by JUnit 4, and was probably first with this feature. Much of the criticism against JUnit's earlier versions concerned creation of several instances of test objects, but TestNG also addresses other items. A TestNG plug-in for Eclipse exists; see http://testng.org/doc/eclipse.html.

Summary

According to the sources described above, it seems likely (but not certain) that JUnit 4 supports unit tests written according to the previous standard. There still remains the possibility of using JUnit 3.8.1. As the Proteios project does not have old unit tests to support, the new standard for writing unit tests is an option, giving the advantages specified in the JUnit 4 documentation. The problem with this approach lies in the fact that existing plug-ins for development tools like Eclipse are adapted to the previous standard. Using TestNG may give rise to problems if web applications are to be tested with jWebUnit, as the latter relies on JUnit. However, combinations of methods cannot be totally ruled out.