Saturday, July 19, 2014

TDD, Hamcrest, Shazamcrest

Recently we have started to try get a more TDD culture started at work, having always believed in thorough testing and decent code coverage it shouldn't have been too hard. However... teaching a old dog new tricks can sometimes require quite a bit of patience. Turns out breaking coding habits formulated of more than a decade of keyboard bashing is harder than it seems.

So with generating an enormous amount of test code, comes the usual task code & test maintenance and reuse.
One of the tools / libraries we have included is Hamcrest, which not only improves the readability of assertion failures, but allows you to create and extend custom matchers, which you can then reuse across multiple test scenarios.

I am not going to go into too much detail on Hamcrest here, where are a bunch of great resources / blogs / tutorials out there.. just a few:

While creating a custom type safe matcher for one of our domain objects, I realised that was insane.. really.. this.getA == that.getA... mmmm no.
So I went searching for something could help and and after a bit, I found: Shazamcrest (bonus points for the name)
What Shazamcrest does is:
Serialize the objects to compare.
Compares them and then on fail throws a ComparisonFailure, which the major IDE's allow you use their build in diff display.

Great... no manual bean compares.
So I add the maven dependency, try it out on our complex domain object....
StackOverflowError.... It was a known limitation at the time. The json provider Shazamcrest was using: 
GSON does not cater for circular reference serialization.

As both Shazamcrest and GSON being opensource, I decided to have a look and see if I could contribute, anything is better that writing a manual bean matcher. After some investigation I found that the guys on the GSON project have created a fix GraphAdapterBuilder, it is just not distributed with the actual library.

So after fork on the Shazamcrest GitHub project, a little bit of code and submitting a pull request:

The guys on the Shazamcrest project very quickly merged my changes in and published a new version to the maven repo (Thanks for that). 
So be sure to use the 0.8 version if you are struggling with circular references.


  1. What tools that you have been using!

  2. Thanks for sending this message.

  3. someone to write your essay is a great way of relieving your academic stress. Despite this fact, many students are still skeptical about hiring someone to do their assignment and homework.

  4. It supports creating customized assertion matchers, allowing match rules to be defined declaratively. Over the years, our Professional Floor Repair Services Chicago IL has consistently grown in service scope and client-base due to its dedication to providing clients with top-notch services every time.


  5. Because my sister is too busy pursuing her further education, I would like to produce these types of proposal report format for her. I don't mind taking on this responsibility, but I'll hire a service rather than doing it myself. Because my sister requires accurate and thorough reports right away, the service I select

  6. It was a very good post indeed and I thoroughly enjoyed surfing your article.  I will surely visit back this blog. Nice and great shared. Thanks for sharing.

  7. Hi
    Thanks for sharing an amazing and informative post. The information shared by you is really useful for me. Keep it up to do great work and hope to see more of your posts in the near future.
    read more: best frameless shower doors


Popular Posts