posted 7 years ago
Okay, let's make a simple example where we mix additive colors together. Let's limit the set of colors to just the primary and the secondary colors:
Now, we want to write a test for the mix() method. We can do this really easily with JUnit's experimental theories API.
For each theory, JUnit creates a Cartesian product of the data points it can assign to the theory's parameters. The assumptions we make at the start of the theory filter out combinations that are not valid to test that particular theory. Then we perform the method under test, and make assertions about the result.
Note that we already wrote 6 test methods to test invariants of one method under test. Each test method tests a very specific invariant, and it's clear from the code what is being tested in each method.
Now, in this case it's very easy to get the data points. But what if we have many data points that we need to generate from configuration data? You can then write a test fixture:
The nice thing about this is that once you have written your fixtures that contain types that are either mocked or loaded from a resource, you can easily fill your data points in many test classes.