More manageable tests with JUnit 4.11
JUnit, the framework for testing Java applications, has improved the handling of parameterised tests in it's latest release, version 4.11. In order to make it easier to distinguish between test cases, a name may be provided to @Parameters
which contains placeholders, {index}
for the index of the test and {0}...{1}...
for the parameter values. When the test is run, this creates more useful test names with details of what parameters were used in the test.
Other changes include the ability to set the execution order of the invocation of test methods. Traditionally, JUnit has invoked tests in the order that the Reflection API returned them, but, as there is no defined order for this returned list, it is somewhat unwise and experience shows that JDK 7 is returning the list in a "more or less random order". Therefore, to ensure reliability, JUnit uses a "deterministic, but not predictable order", MethodSorters.DEFAULT
and, using the @FixMethodOrder
annotation, can override this to the JVM order or ascending method name order.
The new JUnit also upgrades to Hamcrest 1.3, which enhances the matching capabilities in tests. Users of JUnit will find this means that assertThat()
now will not just say what the result was that caused a test to fail, but provide more information to help explain why it failed. Other improvements have been made to Rules, Asset and Assume.
The JUnit framework source and documentation can be found on the project's GitHub pages. It can also be downloaded from the same repository as a zip or jar package. JUnit is licensed under the Common Public Licence 1.0. Further information and code examples for JUnit 4.11's features can be found on Micha Kops developer blog.
(djwm)