"Because well-designed tests build upon each other."

JExample introduces producer-consumer relationships to unit-testing. A producer is a test method that yields its unit under test as return value. A consumer is a test method that depends on one or more producers and (possibly) their return values.

  • Pinpoint failures—JExample skips any test method whose producer has failed.
  • Reuse fixtures—the return value of a producer is injected into its consumers.
  • Works with JUnit—seamless integration in both JUnit and its Eclipse plugin.

JExample is a small framework to write unit tests that build upon each other. As such, JExample diverges from common reading of the isolation guideline of unit testing, which stats that tests should be be separate and apart from the application and all other units. Since its first formulation on the C2 wiki, common understanding of the isolation reading has been extended to concern not only the isolation of units under test but also the isolation of test cases. While we agree to the basic permise of Test-driven Development, that isolating the units under test leads to better design of both application and test code, we dare to disagree regarding the isolation of test cases: on the contrary, it is our conviction that well-designed test cases should build upon each other!

On of the main arguments made in favor of test case isolation is defect localization. In order to quickly locate the root cause of a defect, developers want their attention to be focused on as few failing test as possible. Common wisdom states that defect localization is improved by avoiding dependencies between tests, yet empirical evidence shows that latent dependencies exist anyway even in well-designed test suites [Gaelli 2004]. This suggests that, despite the test isolation guidelines, dependencies between tests are inevitable.

In our work, we propose to improve defect localization by making the latent dependencies between test cases explicit. In a recent case study, we showed that a dependency-aware framework improves performance and defect localization compared to strictly isolated tests [Kuhn 2008].

For example, in the listing below the shouldPop test case depends on the successful outcome of the shouldPush and whenEmpty test cases. Based on this depedency declaration, the jeg framework can automatically determine a suitable order to run the tests, and to skip tests that depend on other failed tests. Developers who use jeg are thus pin-pointed to the relevant failing test case even if the test cases are not isolated.

 import static org.junit.Assert.assertEquals;
 import ch.unibe.jexample.JExample;
 import ch.unibe.jexample.Given;
 import java.util.Stack;
 import org.junit.runner.RunWith;
 import org.junit.Test;
 public class ExampleStack {
     public Stack whenEmpty() {
         Stack stack = new Stack();
         assertEquals(true, stack.isEmpty());
         return stack;
     public Stack shouldPush(Stack stack) {
         assertEquals(false, stack.isEmpty());
         return stack;
     public void shouldPop(Stack stack) {
         Object top = stack.pop();
         assertEquals(true, stack.isEmpty());
         assertEquals("Foo", top);

Dependent tests not only improve defect localization, they also run faster and reduce code duplication. Given all dependencies had been executed successfully, a test T can reuse their fixtures (ie cached return values which are cloned before injection) instead of setting it up from scratch. This helps you to save setup costs and avoids code duplication. If, however, one of the dependencies fails, test T and all its dependents are skipped. This helps you to pin-point failures to their very cause.

JExample is open source and available for download.


  1. Cédric Beust and Hani Suleiman. Next generation Java testing: TestNG and advanced concepts, Addison-Wesley Professional, 2007. URL 
  2. Lea Hänsenberger. JExample. Bachelor's Project, University of Bern, March 2008. PDF 
  3. Lea Hänsenberger, Adrian Kuhn, and Oscar Nierstrasz. Using Dynamic Analysis for API Migration. In Proceedings IEEE Workshop on Program Comprehension through Dynamic Analysis (PCODA 2008), p. 32—36, October 2008. PDF URL 
  4. Lea Hänsenberger. Defect Isolation As Responsibility of the Framework — Automated API Migration from JUnit to JExample. Master's Thesis, University of Bern, September 2009. PDF 
  5. Adrian Kuhn, Bart Van Rompaey, Lea Hänsenberger, Oscar Nierstrasz, Serge Demeyer, Markus Gaelli, and Koenraad Van Leemput. JExample: Exploiting Dependencies Between Tests to Improve Defect Localization. In P. Abrahamsson (Ed.), Extreme Programming and Agile Processes in Software Engineering, 9th International Conference, XP 2008, Lecture Notes in Computer Science p. 73—82, Springer, 2008. DOI PDF 

Last changed by admin on 21 April 2009