JUnit and Other Beasts, Part 4
22 June 2012, by Rowan Hill
In this final installment, we’ll have a look at what else theories might have to offer, and where to find out more.
If you happen to be using .NET for your project, but you like what you see here, fear not! Theories, assumptions and data points all translate directly into NUnit. I haven’t tried this myself, but it looks essentially identical to the JUnit implementation, so all the information in these posts should still be relevant.
As the papers mentioned in Part 2 note, the combination of theories and assumptions lends itself quite nicely to automation exploration of data points. Given a theory that indicates valid input values with assumptions (thereby restricting the possible input space) and which also makes assertions about the results of processing those inputs (thereby restricting the possible result space), the hope is that software could inspect the code and suggest ‘interesting’ data points – i.e. ones which satisfy the assumptions but fail the tests, highlighting either a bug in the production code or a test which could be refined. Indeed, the JUnit release notes mention that JUnit Factory can be used to search for potentially ‘interesting’ data points, rather like Pex in the .NET world. Sadly, it seems the JUnit Factory web presence no longer exists. Another piece of software, tpalus, seems to provide similar functionality, but seems to be quite young and in flux, so perhaps not quite ready for serious use, just yet.
Given there’s very little documentation around for theories, most of the information on theories in these posts has been pieced by having a combination of experimentation and searching around blogs. Some of the posts that are useful are as follows:
- Some articles exploring the basics: Mark Needham, Locke’s Socks, Schauderhaft, ig blog, Chris Kelly. Note that these articles don’t tend to follow the ‘test inverse pairs of operators’ suggestion, and rather use theories as a more convenient parameterised test pattern.
- A slightly more in-depth look how best to use JUnit theories, by David Saff.
- An example of how to use ParameterSuppliers, and another with a fairly detailed real-world example.
That’s all, folks
Hopefully this series of posts has made you aware of just how powerful and flexible modern testing frameworks can be. If you ever find yourself banging your head against a JUnit brick wall, don’t give up in despair – chances are good that there is a solution out there, the trick is just tracking it down.