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.
 

Reference: Introduction To JUnit Theories from our JCG partner Jens Schauder at the Java Advent Calendar blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

2 Responses to "Introduction To JUnit Theories"

  1. Zoltán says:

    I had no idea what JUnit theories were. Now it’s completely clear. A very nice article.

  2. Rodrigo says:

    Great article, thanks!

Leave a Reply


7 + = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books