Mitch Pronschinske is a Senior Content Analyst at DZone. That means he writes and searches for the finest developer content in the land so that you don't have to. He often eats peanut butter and bananas, likes to make his own ringtones, enjoys card and board games, and is married to an underwear model. Mitch is a DZone Zone Leader and has posted 2573 posts at DZone. You can read more from them at their website. View Full User Profile

David Saff Talks About JUnit and the 4.8 Release

12.07.2009
| 9817 views |
  • submit to reddit
JUnit has been an important framework in the evolution of test-driven development (TDD).  The open source unit testing framework has been ported to numerous languages (e.g. PHP (PHPUnit), C# (NUnit), and JVM languages) and can now be found natively in many popular IDEs.  DZone recently talked with David Saff, a JUnit committer and Software Engineer in Test currently working on Google Books, about the newest version of JUnit and the current state of the project.  

Saff said JUnit began with a meeting between its two founders Kent Beck and Erich Gamma.  "I believe that Kent had hand-rolled several similar frameworks for Smalltalk projects before, and Kent and Erich shared:  a concern for quick feedback, and developer craftsmanship, a curiosity to push their Java skills, a trans-Atlantic flight, and a laptop," said Saff.  He explained that JUnit occupies a unique space in the ecosystem of Java test frameworks.  "While JUnit does ship with several built-in extensions (some more experimental than others), what's important to us is that the core of JUnit is the intersection of all possible useful Java test frameworks, not their union," says Saff.  "To test your project, I'll consider it a failure if you can't build what you need on top of JUnit, but not if you have to extend JUnit to get what you need."

JUnit continues to evolve with user feedback.  The newest release, JUnit 4.8 includes categories, which was the most requested feature in a recent survey Saff says.  Categories will allow developers to annotate some texts as belonging to a certain group, including or excluding only those tests.  The Categories runner takes a given set of test classes and runs only the classes and methods that are annotated with the category in the @IncludeCategory annotation, or a subtype of that category. Classes or interfaces can be used as categories. With subytping, if you write @IncludeCategory(SuperClass.class), a test marked @Category({SubClass.class}) will run.  To exclude categories, you can use the @ExcludeCategory annotation.

Here is an example of category exclusion:

public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ }

public class A {
@Test
public void a() {
fail();
}

@Category(SlowTests.class)
@Test
public void b() {
}
}

@Category({SlowTests.class, FastTests.class})
public class B {
@Test
public void c() {

}
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b, but not A.a or B.c
}

Saff says, "4.8 is a quick release to get some community experience with a proposed API for categories: we're anxious to hear how developers use it."  For those who don't already have JUnit provided natively in their development environment (Eclipse, IDEA, NetBeans, or even Maven), there are command-line instructions available at Sourceforge.  The JUnit Cookbook will help developers get started with unit testing and JUnit.

Saff said development on JUnit is "slow and steady."  He and Kent Beck pair program about once a week he says.  DZone asked Saff what's next on the list of upcoming JUnit features.  There were two main features he mentioned:

1) A new assertion mechanism that takes advantage of Java's built-in boolean methods:
  • assertThat(myList).contains(defaultElement) =>
  • Failed: <["sometimes", "rarely", "hardly ever"]> does not contain <always>

2) Better support for multi-core processors

Saff also says that the JUnit team is excited about the reuse of JUnit for newer JVM languages like Scala, Groovy, and JRuby.  "We look forward to seeing how this evolves," he says.  Saff believes that JUnit, "celebrates developers testing their own code, and strives to make it easy and fun."

Comments

Rogerio Liesenfeld replied on Mon, 2009/12/07 - 6:16pm

I think they almost got it right:

1) The @Category annotation should be allowed on annotation types as well. That way, a user could define and use an @FastTests annotation instead of writing "@Category(FastTests.class)", which is more verbose.
2) The "Categories" test runner should not require the use of "@SuiteClasses". Instead, it should simply run all tests in the indicated categories.

Pavan Kumar replied on Wed, 2009/12/16 - 5:45am

I want know about JUnit

Mitch Pronschinske replied on Thu, 2009/12/17 - 11:24am in response to: Pavan Kumar

Here's a link to "Getting Started" with JUnit along with some helpful documentation.  :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.