Improving Java unit test readability and maintainability

The last year has seen me working with Ruby rather than Java and I have really come to like the the way tests (or rather specs) are structured in RSpec and the general way BDD approaches tests (specs).

RSpec splits the behaviour of a class up into what happens in different contexts. An example probably explains things better, taking a trivial case of a stack (I'll get to Java in a minute but even if you don't know Ruby I think this code should be understandable):

RUBY:
  1. require File.dirname(__FILE__) + "/stack"
  2.  
  3. context "A populated Stack" do
  4.   setup do
  5.     @stack = Stack.new
  6.     ["a","b","c"].each { |x| @stack.push x }
  7.   end
  8.  
  9.   specify "should add to the top when sent #push" do
  10.     @stack.push "d"
  11.     @stack.peek.should == "d"
  12.   end
  13.  
  14.   specify "should return the top item when sent #peek" do
  15.     @stack.peek.should == "c"
  16.   end
  17.  
  18.   specify "should NOT remove the top item when sent #peek" do
  19.     @stack.peek.should == "c"
  20.     @stack.peek.should == "c"
  21.   end
  22.  
  23.   specify "should return the top item when sent #pop" do
  24.     @stack.pop.should == "c"
  25.   end
  26.  
  27.   specify "should remove the top item when sent #pop" do
  28.     @stack.pop.should == "c"
  29.     @stack.pop.should == "b"
  30.   end
  31. end
  32.  
  33. context "An empty stack" do
  34.   setup do
  35.     @stack = Stack.new
  36.   end
  37.  
  38.   specify  "should be empty" do
  39.     @stack.should be_empty
  40.   end
  41.  
  42.   specify "should no longer be empty after #push" do
  43.     @stack.push "anything"
  44.     @stack.should_not be_empty
  45.   end
  46.  
  47.   specify "should complain when sent #peek" do
  48.     lambda { @stack.peek }.should raise_error(StackUnderflowError)
  49.   end
  50.  
  51.   specify "should complain when sent #pop" do
  52.     lambda { @stack.pop }.should raise_error(StackUnderflowError)
  53.   end
  54. end
  55.  
  56. context "An almost empty stack (with one item)" do
  57.   setup do
  58.     @stack = Stack.new
  59.     @stack.push 3
  60.   end
  61.  
  62.   specify "should not be empty" do
  63.     @stack.should_not be_empty
  64.   end
  65.  
  66.   specify "should remain not empty after #peek" do
  67.     @stack.peek
  68.     @stack.should_not be_empty
  69.   end
  70.  
  71.   specify "should become empty after #pop" do
  72.     @stack.pop
  73.     @stack.should be_empty
  74.   end
  75. end

So a stack should behave in a particular way:

A populated Stack
- should add to the top when sent #push
- should return the top item when sent #peek
- should NOT remove the top item when sent #peek
- should return the top item when sent #pop
- should remove the top item when sent #pop

An empty stack
- should be empty
- should no longer be empty after #push
- should complain when sent #peek
- should complain when sent #pop

An almost empty stack (with one item)
- should not be empty
- should remain not empty after #peek
- should become empty after #pop

(incidentally the above is output generated from the spec tool, a bit like generating JavaDocs from Java code)

I think splitting the behaviour up into these different contexts greatly improves the readability and maintainability of both the tests themselves and the code under test. But how to do it in Java?

It's possible to use RSpec to test Java code because you can run RSpec using JRuby, however I'm not sure if it is wise to drive the development of code in one language with tests in a different language, Ruby and Java approach numerous things very differently.

Now it would be possible to have numerous JUnit test classes for a given class, each one for a different context but this has it's drawbacks, when you make a change to your class you'd need to run numerous test cases, which you could get around by having a master testcase with a suite method that includes all the others, but then you have to maintain the list of testcases in the suite() method and it's all too easy for the list to get out of sync with the actual test cases. JUnit 4 provides a solution, you can use the Enclosed runner. The code below is an equivalent example to the RSpec one in Java.

JAVA:
  1. import static org.junit.Assert.assertThat;
  2.  
  3. import static org.hamcrest.CoreMatchers.is;
  4.  
  5. import java.util.EmptyStackException;
  6.  
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.junit.runners.Enclosed;
  11.  
  12. @RunWith(Enclosed.class)
  13. public class TestStack {
  14.  
  15.         public static class AnEmptyStack {
  16.  
  17.                 private Stack<Object> stack = null;
  18.  
  19.                 @Before
  20.                 public void setupAnEmptyStack() {
  21.                         stack = new Stack<Object>();
  22.                 }
  23.  
  24.                 @Test
  25.                 public void shouldBeEmpty() {
  26.                         assertThat(stack.isEmpty(), is(false));
  27.                 }
  28.  
  29.                 @Test(expected = EmptyStackException.class)
  30.                 public void shouldComplainWhenPeeked() {
  31.                         stack.peek();
  32.                 }
  33.  
  34.                 @Test(expected = EmptyStackException.class)
  35.                 public void shouldComplainWhenPoped() {
  36.                         stack.pop();
  37.                 }
  38.  
  39.                 @Test
  40.                 public void shouldNotBeEmptyAfterItemIsPushedOntoIt() {
  41.                         stack.push(new Object());
  42.                         assertThat(stack.isEmpty(), is(false));
  43.                 }
  44.         }
  45.         public static class AStackThatIsNeitherEmptyNorFull {
  46.                 private Stack<String> stack = null;
  47.  
  48.                 @Before
  49.                 public void setupAStackWithThreeItems() {
  50.                         stack = new Stack<String>();
  51.                         stack.push("one");
  52.                         stack.push("two");
  53.                         stack.push("three");
  54.                 }
  55.  
  56.                 @Test
  57.                 public void shouldAddToTheTopWhenSentAPush() {
  58.                         stack.push("four");
  59.                         assertThat(stack.peek(), is("four"));
  60.                 }
  61.  
  62.                 @Test
  63.                 public void shouldReturnTheTopItemWhenSentAPeek() {
  64.                         assertThat(stack.peek(), is("three"));
  65.                 }
  66.  
  67.                 @Test
  68.                 public void shouldNotRemoveTheTopItemWhenSentAPeek() {
  69.                         assertThat(stack.peek(), is("three"));
  70.                         assertThat(stack.peek(), is("three"));
  71.                 }
  72.  
  73.                 @Test
  74.                 public void shouldReturnTheTopItemWhenSentAPop() {
  75.                         assertThat(stack.peek(), is("three"));
  76.                 }
  77.  
  78.                 @Test
  79.                 public void shouldRemoveTheTopItemWhenSentAPop() {
  80.                         assertThat(stack.pop(), is("three"));
  81.                         assertThat(stack.peek(), is("two"));
  82.                 }
  83.         }
  84.         public static class AStackThatContainsOnlyOneItem {
  85.                 private Stack<String> stack = null;
  86.  
  87.                 @Before
  88.                 public void setupAStackWithOneItem() {
  89.                         stack = new Stack<String>();
  90.                         stack.push("one");
  91.                 }
  92.  
  93.                 @Test
  94.                 public void shouldNotBeEmpty() {
  95.                         assertThat(stack.isEmpty(), is(false));
  96.                 }
  97.  
  98.                 @Test
  99.                 public void shouldNotBeEmptyAfterAPeek() {
  100.                         stack.peek();
  101.                         assertThat(stack.isEmpty(), is(false));
  102.                 }
  103.  
  104.                 @Test
  105.                 public void shouldBeEmptyAfterAPop() {
  106.                         stack.pop();
  107.                         assertThat(stack.isEmpty(), is(true));
  108.                 }
  109.         }
  110. }

As an aside I've also used the new assertThat() method and Hamcrest matchers as you get significantly better error messages when your tests fail that way.

To get a plain test output you could probably write a XSLT stylesheet that parses the test report, the stylesheet Kerry produced for use with Junit 3.x tests maybe a good starting point, or write a custom formatter and specify that in your JUnit Ant task.

There are also the JBehave and JDave BDD frameworks for Java which would be worth looking at if you like the look of BDD.

Leave a Reply