This week I read an article about the Benefits of using assertThat over other Assert Methods in Unit Tests. Since in our reading materials prof. Wurst is using assertThat instead of traditional Asserts, I think it is time to familiarize myself with the new asserting statements.
According to the article, AssertThat method incorporates the use of the hamcrest library and is a much improved way to write assertions. It uses what’s called matchers which are self-contained classes which have static methods that get used with the assertThat method. These static methods can be chained which gives a lot of flexibility over using the old assert methods.
Until now I have been using assertequals while writing my test cases. The biggest issue I had was to remember the correct ordering of (expected, actual) in assertEquals(expected, actual) statement. The keywords ‘expected’ and ‘actual’ used were not even informative enough to determine what exactly goes in it. With the use of assertThat method I don’t see such complications. The first benefit I see is that assertThat is more readable than the other assert methods. Writing same assertion with assertThat looks like:
assertThat(actual, is(equalTo(expected)))
It reads more like a sentence. Assert that the actual value is equal to the expected value, which make more sense.
Similarly, to check for not equals, used to be:
assertFalse(expected.equals(actual))
Now with the use of assertThat it will be:
assertThat(actual, is(not(equalTo(expected)))
The “not” method can surround any other method, which makes it a negate for any matcher. Also, the matcher methods can be chained to create any number of possible assertions. There’s an equivalent short-hand version of the above equality methods which saves on typing:
assertThat(actual, is(expected))
assertThat(actual, is(not(expected)))
Another benefit to assertThat is generic and type-safe. The below given example of assertEquals compiles, but fails:
assertEquals(“abc”, 123)
The assertThat method does not allow this as it typed, so the following would not compile:
assertThat(123, is(“abc”))
This is very handy as it does not allow comparing of different types. I find this a welcome change.
Another benefit to using assertThat are much better error messages. Below is a common example of the use of assertTrue and its failure message.
assertTrue(expected.contains(actual))
java.lang.AssertionError at …
The problem here is that the assertion error doesn’t report the values for expected and actual. Granted the expected value is easy to find, but a debugging session will probably be needed to figure out the actual value.
The same test using assertThat:
assertThat(actual, containsString(expected))
java.lang.AssertionError: Expected: a string containing “abc”
In this case, both values are returned in the error message. I think this a much better since in many cases a developer can look at the error message and figure out right away what they did wrong rather than having to debug to find the answer. That saves time and hassle. I am excited to get my hands on with the new AssertThat method in my upcoming days.
From the blog CS@Worcester – Not just another CS blog by osworup007 and used with permission of the author. All other rights reserved by the author.