JUnit Theories

http://www.javacodegeeks.com/2013/12/introduction-to-junit-theories.html

Introduction To JUnit Theories

<iframe id="google_ads_iframe_/70432299/JCG_In-post_0" style="border-width: 0px; vertical-align: bottom;" name="google_ads_iframe_/70432299/JCG_In-post_0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="300" height="250"></iframe>

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:

1 @Test
2  public void a_plus_b_is_greater_than_a_and_greater_than_b(){
3    int a = 2;
4    int b = 3;
5    assertTrue(a + b > a);
6    assertTrue(a + b > b);
7  }

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):

1 @Test
2  public void a_plus_b_is_greater_than_a_and_greater_than_b_multiple_values() {
3     List<Integer> values = Arrays.asList(12300400000);
4     for (Integer a : values)
5       for (Integer b : values) {
6          assertTrue(a + b > a);
7          assertTrue(a + b > b);
8       }
9     }

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:

01 import org.junit.experimental.theories.DataPoints;
02 import org.junit.experimental.theories.Theories;
03 import org.junit.experimental.theories.Theory;
04 import org.junit.runner.RunWith;
05  
06 import static org.junit.Assert.assertTrue;
07  
08 @RunWith(Theories.class)
09 public class AdditionWithTheoriesTest {
10  
11   @DataPoints
12   public static int[] positiveIntegers() {
13        return new int[]{
14                         1101234567};
15   }
16  
17   @Theory
18   public void a_plus_b_is_greater_than_a_and_greater_than_b(Integer a, Integer b) {
19       assertTrue(a + b > a);
20       assertTrue(a + b > b);
21   }
22 }

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:

01 @RunWith(Theories.class)
02  public class AdditionWithTheoriesTest {
03  
04   @DataPoints
05   public static int[] integers() {
06      return new int[]{
07                    -1, -10, -1234567,1101234567};
08   }
09  
10   @Theory
11   public void a_plus_b_is_greater_than_a_and_greater_than_b(Integer a, Integer b) {
12      Assume.assumeTrue(a >0 && b > 0 );
13      assertTrue(a + b > a);
14      assertTrue(a + b > b);
15   }
16  
17   @Theory
18   public void addition_is_commutative(Integer a, Integer b) {
19      assertTrue(a + b == b + a);
20   }
21 }

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:

1 @DataPoints
2   public static int[] integers() {
3      return new int[]{
4                      0, -1, -10, -1234567,1101234567, 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.
 

你可能感兴趣的:(JUnit)