JUnit and Hamcrest: Improving On assertEquals
The next code listing is a simple IntegerArithmetic
class that has one method that needs to be unit tested.
IntegerArithmetic.java
package dustin.examples; /** * Simple class supporting integer arithmetic. * * @author Dustin */ public class IntegerArithmetic { /** * Provide the product of the provided integers. * * @param integers Integers to be multiplied together for a product. * @return Product of the provided integers. * @throws ArithmeticException Thrown in my product is too small or too large * to be properly represented by a Java integer. */ public int multipleIntegers(final int ... integers) { int returnInt = 1; for (final int integer : integers) { returnInt *= integer; } return returnInt; } }
A common approach for testing one aspect of the above method is shown next.
/** * Test of multipleIntegers method, of class IntegerArithmetic, using standard * JUnit assertEquals. */ @Test public void testMultipleIntegersWithDefaultJUnitAssertEquals() { final int[] integers = {2, 3, 4 , 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15; final int result = this.instance.multipleIntegers(integers); assertEquals(expectedResult, result); }
In the fairly typical unit test example shown above, JUnit’s assertEquals is called in a fluent fashion because of the static import of org.junit.Assert.* (not shown). However, recent versions of JUnit (JUnit 4.4+) have begun including Hamcrest core matchers and this allows for an even more fluent test as depicted in the next code snippet.
/** * Test of multipleIntegers method, of class IntegerArithmetic, using core * Hamcrest matchers included with JUnit 4.x. */ @Test public void testMultipleIntegersWithJUnitHamcrestIs() { final int[] integers = {2, 3, 4 , 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15; final int result = this.instance.multipleIntegers(integers); assertThat(result, is(expectedResult)); }
In this example, JUnit’s assertThat (also available as part of the static import of org.junit.Assert.*
since JUnit 4.4) is used in conjunction with the included Hamcrest core matcher is()
. It’s certainly a matter of taste, but I prefer this second approach as more readable to me. Asserting that something (the result) is something else (the expected) seems more readable and more fluent than the older approach. It can sometimes be tricky to remember whether to list the expected or actual result first when using assertEquals
and the combination of assertThat and is()
makes for a little less work when I write and read tests. Even a little less work, especially when multiplied by numerous tests, is welcome.
Reference: Improving On assertEquals with JUnit and Hamcrest from our JCG partner Dustin Marx at the Inspired by Actual Events blog.
I don’t like all the static imports of Hamcrest. FEST is a lot easier to use and archives the same thing: http://docs.codehaus.org/display/FEST/Fluent+Assertions+Module
Exactly FEST is much much better