Need to test a class or method with various values? You can make use of parameterized JUnit tests to accomplish this. And since I’m currently learning Groovy, I’m taking this chance to do it in this neat language (and to keep it as a reminder for some language constructs).
You can make use of parameterized tests using the appropriate JUnit test runner. In case of the following code listing, I’m verifying that my regular expression for Java package names is correct (or at least that it detects a certain amount of invalid ones…). Test input data is obtained from the data() method and passed to the test’s constructor. In the testInvalidPackages() method I’m simply making use of the instance variable pckg, i.e., changing the value.