Tuesday, August 23, 2011

JUnit 4.9 released

Over a year in the making, we are happy to announce the release of JUnit 4.9.

This release's theme is Test-class and suite level Rules. The release notes are copied in part below, and can be read in full here.

Go ahead and download, or merely let maven do the work for you (as we are now directly releasing to sonatype's maven repository on the same day we release the jars).

Following this release, work on JUnit and related code will continue on three fronts:

  • Bug fixes against existing functionality will be committed to the 4.9.1 branch.
  • New core features will be committed on the 4.10 branch.
  • We have created a new junit.contrib project to be a home to new experimental features, features that use additional dependencies, and features that are outside of JUnit's core mission. As a first example, Thomas Mueller has created assertthrows, a package that uses all kinds of JDK trickery to allow direct assertions like this:
    
      List<String> emptyList = new ArrayList<String>();
      assertThrows(emptyList).get(0);
    
We are excited that github allows extensive community participation and discussion in the ongoing development. Please pitch in! Remember, JUnit celebrates programmers testing their own software. Let's build something.

JUnit 4.9 release notes (highlights)

ClassRule

The ClassRule annotation extends the idea of method-level Rules, adding static fields that can affect the operation of a whole class. Any subclass of ParentRunner, including the standard BlockJUnit4ClassRunner and Suite classes, will support ClassRules.

For example, here is a test suite that connects to a server once before all the test classes run, and disconnects after they are finished:

@RunWith(Suite.class)
@SuiteClasses({A.class, B.class, C.class})
public class UsesExternalResource {
    public static Server myServer= new Server();

    @ClassRule
    public static ExternalResource resource= new ExternalResource() {
        @Override
        protected void before() throws Throwable {
            myServer.connect();
        };

        @Override
        protected void after() {
            myServer.disconnect();
        };
    };
}

TestRule

In JUnit 4.9, fields that can be annotated with either @Rule or @ClassRule should be of type TestRule. The old MethodRule type, which only made sense for method-level rules, will still work, but is deprecated.

Most built-in Rules have been moved to the new type already, in a way that should be transparent to most users. TestWatchman has been deprecated, and replaced by TestWatcher, which has the same functionality, but implements the new type.

Maven support

Maven bundles have, in the past, been uploaded by kind volunteers. Starting with this release, the JUnit team is attempting to perform this task ourselves.

LICENSE checked in

The Common Public License that JUnit is released under is now included in the source repository.

3 comments:

  1. Nice work!

    We should look into integrating the ClasspathSuite project (http://www.johanneslink.net/projects/cpsuite.jsp) into junit. So instead of explicitly specifying every class like @SuiteClasses({A.class, B.class, C.class}), you instead specify a wildcard string like this: @SuiteClasses(com.company.*). Verbosity required in the suites is still a error prone.

    ReplyDelete
  2. Anonymous,

    It would be nice to have ClasspathSuite support. However, the current implementation of that runner follows an annotation-heavy style that the rest of JUnit has moved away from. If you want to talk about designs, bring it up on the list: junit@yahoogroups.com. Thanks!

    ReplyDelete