Over a million developers have joined DZone.
Platinum Partner

David Saff Talks About JUnit and the 4.8 Release

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

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."

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}