Tuesday, December 17, 2013

Introduction To JUnit Theories

Have you ever read a mathematical theory?

It typically reads something like this:

For all a, b > 0  the following is true: a+b > a and a+b > b

Just typically the statements are more difficult to understand.

There is something interesting about this kind of statement: It holds for EVERY element (or combination of elements) of a rather large (infinite in this case) set.

Compare that to the statement a typical test makes:

 @Test
 public void a_plus_b_is_greater_than_a_and_greater_than_b(){
   int a = 2;
   int b = 3;
   assertTrue(a + b > a);
   assertTrue(a + b > b);
 }

This is just a statement about a single element of the large set we talked about. Not very impressive. Of course we can fix that somewhat by looping over the test (or using parameterized tests):


 @Test
 public void a_plus_b_is_greater_than_a_and_greater_than_b_multiple_values() {
    List<Integer> values = Arrays.asList(1, 2, 300, 400000);
    for (Integer a : values)
      for (Integer b : values) {
         assertTrue(a + b > a);
         assertTrue(a + b > b);
      }
    }

Of course this still only tests a few values, but it also became pretty ugly. We are using 9 lines of code to  test what a mathematician writes in a single line! And the main point that this relation ship should hold for any value a,b is completely lost in translation.

But there is hope: JUnit Theories. Let's see how the test looks like with that nifty tool:
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;

@RunWith(Theories.class)
public class AdditionWithTheoriesTest {

  @DataPoints
  public static int[] positiveIntegers() {
       return new int[]{
                        1, 10, 1234567};
  }

  @Theory
  public void a_plus_b_is_greater_than_a_and_greater_than_b(Integer a, Integer b) {
      assertTrue(a + b > a);
      assertTrue(a + b > b);
  }
}
With JUnit Theories the test gets split in two separate parts: a method providing data points i.e. values to be used for tests, and the theory itself. The theory looks almost like a test, but it has a different annotation (@Theory) and it takes parameters. The theories in a class get executed with every possible combination of data points.

This means that if we have more then one theory about our test subject we only have to declare the data points once. So let's add the following theory, which should be true for addition: a + b = b + a So we add the following theory to our class
@Theory public void addition_is_commutative(Integer a, Integer b) { assertTrue(a + b == b + a); }
This works like a charm and one can start to see that this actually saves some code as well, because we don't duplicate the data points. But we only test with positive integers, while the commutative property should hold for all integers! Of course our first theory still only holds for positive numbers

There is a solution for this as well: Assume. With assume you can check precondition for your theory. If it isn't true for a given parameter set, the theory gets skipped for that parameter set. So our test now looks like this:

 @RunWith(Theories.class)
 public class AdditionWithTheoriesTest {

  @DataPoints
  public static int[] integers() {
     return new int[]{
                   -1, -10, -1234567,1, 10, 1234567};
  }

  @Theory
  public void a_plus_b_is_greater_than_a_and_greater_than_b(Integer a, Integer b) {
     Assume.assumeTrue(a >0 && b > 0 );
     assertTrue(a + b > a);
     assertTrue(a + b > b);
  }

  @Theory
  public void addition_is_commutative(Integer a, Integer b) {
     assertTrue(a + b == b + a);
  }
}

This makes the tests nicely expressive.

The separation of test data from test/theory implementation can have another positive effect apart from brevity: You might start to think about you test data independent of the actual stuff to test.

Lets do just that. If you want to test a method that takes an integer argument, what integers would be likely to cause problems? This is my proposal:


  @DataPoints
  public static int[] integers() {
     return new int[]{
                     0, -1, -10, -1234567,1, 10, 1234567, Integer.MAX_VALUE, Integer.MIN_VALUE};}

Which of course causes a test failure in our example. If you add a positive integer to Integer.MAX_VALUE you get an overflow! So we just learned that our theory in its current form is wrong! Yeah I know this is obvious, but have a look at the tests in your current project. Do all the tests that use Integers test with MIN_VALUE, MAX_VALUE, 0, a positive and a negative value? Yeah, thought so.

What about more complex objects? Strings? Dates? Collections? Or domain objects? With JUnit Theories you can setup test data generators once that create all the scenarios that are prone to create problems and then reuse those in all your tests using theories. It will make your tests more expressive and improve the probability of finding bugs.

No comments:

Post a Comment