Junit 4

I’ve got a little project at work that’s completely new (hurrah!), and using Java 5. So, I thought I’d give the latest Junit a try. Junit 4 is quite a departure from the older versions. It relies on features only present in Java 5, like annotations. But this does free up the tests from having to inherit from TestCase.

This is the simplest test case.

  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;

  public class MyTest {
    public void setUp() throws Exception {
      // ...

    public void alwaysTrue() {

You can make this slightly nicer by using static imports for the assertions. I didn’t because I wanted eclipse to complete them and I couldn’t auto-discover them as easily. I’ll probably switch shortly.

The annotations are also used for stating which exceptions you wish to catch.

All in all, I’m quite impressed. It’s about as simple as it gets in Java.