JUnit 4.0

| | bookmark | email | 2 comments

There have been lately lots of posts on junit group about JUnit next release (4.0 - based on a single phrase poured inside an interview). Finally, two days ago somebody watching Kent's testing presentation on Agitar, pulled the curtains and let us know that the code is already in CVS on a special branch. I felt a kind of enthusiasm hearing this, as JUnit was almost frozen for a long time. I have checked out the sources, and after a small digging I have found a set of interesting points that determined me to do a comparison between and the next JUnit (and in a way these represent a good validation of our ideas).

Now let me step through the new things to come in the next version and do a comparison with what TestNG is already offering (for detailed documentation of TestNG please consult the official site):

  1. TestNG has introduced annotations (JDK1.5 annotations and respectively javadoc-based annotations) for marking the important pieces of your tests (@Test for test methods, @Configuration for configuration methods - setUp/tearDown, @ExpectedExceptions for exception testing, @Factory for test factories and @Parameter for passing parameters to tests.
    JUnit 4 biggest change is the adoption of annotations (I will present them later, throughout this entry).
  2. TestNG has introduced 4 different levels of configuring a test: suite, test, class and method (in the form of before/afterSuite, before/afterTest, before/afterTestClass and before/afterTestMethod), from which JUnit 3.x supported only suite and method levels.
    JUnit 4 is bringing the @BeforeClass and @AfterClass annotations for marking before/after class configuration methods.
  3. A test method in TestNG is defined using the @Test whose definition is:
    JUnit 4 is introducing a @Test annotation too:
    From the above definitions, we see that in TestNG a test may be enabled/disable, feature present in JUnit 4 in the form of the annotation @Ignore for disabling a test method.

    In TestNG, you can provide a timelimit after which the test method if not finished is considered failed: timeOut.
    JUnit @Test annotations contains a timeout.
    On the same TestNG @Test definition you can see that you can provide the number of times you want the test method invoked.
    JUnit 4 is bringing RepeatedTest decorator [1], that will allow to run repeatedly a test.
  4. TestNG tests are run inside independent thread in order to allow parallelism and also the above mentioned timeOut feature (see docs).
    In JUnit 4 this is a new decorator ActiveTestSuite [1] that runs each test in a separate thread and waits untill all threads have terminated (javadoc).
  5. An interesting feature introduced by TestNG was the @ExpectedException annotation that allows an automatic test for exceptions.
    In JUnit 4 @Test annotation definition you can see the Class expected() default None.class;.
  6. TestNG is providing two kinds of listeners allowing to extend the functionality: ITestListener (acknowledge upon test method events) and ISuiteListener (triggered upon suite events).
    We can find the ITestListener synonim in JUnit: TestListener (triggered upon test method events).
There is one more, but this is not yet implemented (or I have missed it). TestNG has introduced the possiblity to group together your tests and at runtime filter/run just a subset of these. This was one of the most appreciated features as far as I can tell from the feedback we have received. In JUnit 4 todo I am reading: test categorization & filtering (<- seems like a runner issue) @Category(short, integration) @Test(category=windowsOnly).

Update: thanks to Kent's clarifications I have to correct the above information about RepeatedTest and ActiveTestSuite. These are available since JUnit 3.x and they are included in JUnit 4 just for backward compatibility.