Wednesday
Oct302013

Asserting Exceptions With JUnit Rules: IsEqual Matcher

In my first post on asserting exceptions with JUnit’s Rules feature, I showed how to test that a specific type of exception is thrown with expected substrings in the error message. In my second post, I showed how we can write a custom Matcher to inspect the contents of the exception. In this post, I’ll show you how to take advantage of the stock IsEqual matcher to accomplish the same task, but with less work.

IsEqual Matcher

This Matcher is straightforward - it evaluates whether two objects are equal by calling equals(Object) on one of the objects that isn’t null, passing in the other. So, to use it with our custom exception, we’ll need to make sure that our equals(Object) method correctly evaluates two of its instances.

The default behavior of equals(Object obj) is to check if two objects are the same instance:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

This won’t help us here, because we’ll be comparing exceptions thrown in our code with one we instantiated in our unit test. We’re going to have to implement equals(Object) ourselves.

Implementing equals(Object)

Here’s my simple custom exception:

You have to be careful when implementing either either equals(Object) or hashCode(). The rule is that if you implement either of these methods, then you need to implement both. Two objects that are equal must have the same hash code. If they don’t, and you try to add two distinct, but equal instances of a class to a Map, they’d both be accepted. This is because internally, the Map stores the items in “buckets” based on their hash codes, and then only has to check equality within a bucket. If two objects have different hash codes, then they won’t be compared to each other.

Your IDE should have the ability to generate what we need here. If you’re using Eclipse (I recommend the STS version), right-click in the source file, select “Source”, and the select “Generate hashCode() and equals()…”

After selecting that option, choose which private members will be used in the two methods. I recommend selecting “Use blocks in ‘if’ statements” in order to help wrong code look wrong, should someone modify these methods down the road.

Here’s our final ErrorCodeException class with the newly generated code:

Verifying equals(Object) and hashCode()

Even though we generated this code, we still need to test it. Here’s the test fixture for ErrorCodeException:

Using the IsEqual Matcher In Our Unit Tests

Now that we’ve implemented equals(Object) and hashCode() for our custom exception, we can use the IsEqual Matcher to setup an expectation for a specific exception.

In the first test, I create an IsEqual Matcher with the exception that I want to compare the thrown exception to. No custom Matcher was required, and my custom exception is now more useful because of it.

In my second test, I include the “old way” of checking exceptions to demonstrate how much easier and more readable exception tests are when using JUnit’s Rules feature.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (139)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: hop over to here
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: no no pro kase
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: phil st ores
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Addieup
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: check here
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: kinderm8
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Samsung SD card
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: My blog
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: gta 5 hack
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: traffic school
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: web host
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: fxrelax.com
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: high pr backlinks
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Www.csracpoa.com
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: GoPro Bag
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: bookmarksseo.com
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: GoPro accessories
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: rudegram.com
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: GoPro Selfie Stick
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: shahedbam.ir
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: you could try here
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: babies love toys
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: www.zahraj.org
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: agen poker
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: ceme online
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Slimming Outfits
    ... weight loss - The tests show that they do not get you kickstarted in losing weight. Then the best choice, for those who have failed to smooth over love handles. If you occasionally hear unpal... Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit ... ...
  • Response
    Response: 64gb Micro SD
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Sell BHPH Notes
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: agen sbobet online
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: poker indonesia
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: bandar bola euro
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: GoPro carrying bag
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: try this out
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: hulk-games.org
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: www.gpugrid.net
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: 32gb micro SD card
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    matcher
  • Response
    Response: GoPro Stick
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: good web design
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: watch here
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: hosting approach
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: kursus sepatu
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: replace Roof
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Business Cards
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: clash clans cheats
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: yeezy 350
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: 1012.chinaccnet.cn
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: read the article
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: moneymaker
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: dmccray.com
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: Agen Domino Qq
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual Matcher
  • Response
    Response: try what she says
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: trumpipedia.Com
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: zero kilometer
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: Togel online
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: ebook reader
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: made my day
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: Click To See More
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual
  • Response
    Response: next Car
    Blake Caldwell - /dev/blake/blog - Asserting Exceptions With JUnit Rules: IsEqual

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« Exercising Caution With Hibernate Entities | Main | Asserting Exceptions With Junit Rules: Custom Matchers »