JUnit Hamcrest: Matching a Group of Map Properties from Any Map within a List

Let’s say that you have an ArrayList of LinkedHashMaps. Let’s further say that you would like to find any Map within that List, which contains a set of Properties. Here is how you can write a Unit Test with Hamcrest Matchers to do this:

 import org.hamcrest.Matchers;
 import static org.hamcrest.collection.IsMapContaining.hasEntry;
 import java.util.LinkedHashMap;
 import java.util.List;
 

 public class MyTest {
     @Test
     public void testListOfMap() {
         List<LinkedHashMap<String, Object>> responseList = (List<LinkedHashMap<String, Object>>) getResponse();
         assertThat(responseList,
             Matchers.hasItem(Matchers.<LinkedHashMap<String, Object>>allOf(hasEntry("ticker", "PGRNX"),
                 hasEntry("cusip", "704223783"), hasEntry("name", "Pax Global Environmental Mrkts"),
                 hasEntry("scale", 3), hasEntry("closed", false))));
         assertThat(responseList,
             Matchers.hasItem(Matchers.<LinkedHashMap<String, Object>>allOf(hasEntry("ticker", "VTIAX"),
                 hasEntry("cusip", "921909818"), hasEntry("name", "Vanguard Total Intl Stock Index Admiral"),
                 hasEntry("scale", 3), hasEntry("closed", false))));
     }
 } 

Here we can see that we are checking that the list contains two Maps. Each time we are looking for a Map that matches on five different key-value pairs, such as “ticker”, “cusp”, “name”, “scale” and “closed”. Here you can see we are checking a variety of data types like String, Integer, and Boolean. As a result, we must properly parameterize the Matcher “allOf”; otherwise, we would see the following compilation error:

The method of(Matcher<? super T>, Matcher<? super T>, Matcher<? super T>, Matcher<? super T>, Matcher<? super T>) in the type Matchers is not applicable for the arguments (Matcher<Map<? extends String,? extends String>>, Matcher<Map<? extends String,? extends String>>, Matcher<Map<? extends String,? extends String>>, Matcher<Map<? extends String,? extends Integer>>, Matcher<Map<? extends String,? extends Boolean>>)

We use “allOf” to make sure that all five properties match on a given Map. We use the Matcher “hasItem”, because we want to find at least one Map that meets the criteria of matching on all five properties. We do not use the “contains” Matcher, because that would check that every Map meets has all five of the properties matching.

146 Comments:

  1. You have brought up a very wonderful points, thankyou for the post.

  2. I’ve been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the net will be much more useful than ever before.

  3. Heya this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

  4. Along with every thing which seems to be building within this area, your viewpoints are generally somewhat stimulating. Having said that, I appologize, but I can not give credence to your entire plan, all be it refreshing none the less. It appears to me that your commentary are generally not totally validated and in simple fact you are your self not wholly confident of your argument. In any case I did appreciate reading it.

  5. I’m so glad I stumbled upon this article. It was exactly what I needed to read!

  6. The way you articulate your thoughts is as refreshing as the first sip of coffee in the morning.

  7. The information you’ve shared has been a revelation for me. Incredibly enlightening!

  8. Reading your post was like going on a first date with my mind. Excited for the next rendezvous.

  9. Your insights are like a sunrise, bringing light and warmth to new ideas.

  10. You weave words with the skill of a master tailor, crafting pieces that fit the mind perfectly.

  11. Both informative and thought-provoking, as if my brain needed the extra workout.

  12. Your blog has become a go-to resource for me. The effort you put into your posts is truly appreciated.

  13. I appreciate the balance and fairness in your writing. Great job!

  14. Thank you for adding value to the conversation with your insights.

  15. This is one of the most comprehensive articles I’ve enjoy reading on this topic. Kudos!

  16. This post is a testament to your expertise and hard work. Thank you!

  17. Your attention to detail is as attractive as it is thorough. I appreciate a person who notices the little things.

  18. This was a thoroughly insightful enjoy reading. Thank you for sharing your expertise!

  19. Your writing style is captivating! I was engaged from start to finish.

  20. This post is packed with useful insights. Thanks for sharing your knowledge!

  21. Your insights have added a lot of value to my understanding. Thanks for sharing.

  22. I’m bookmarking this for future reference. Your advice is spot on!

  23. Thank you for the hard work you put into this post. It’s much appreciated!

  24. This article was a delightful enjoy reading. Your passion is clearly visible!

  25. I appreciate the unique viewpoints you bring to your writing. Very insightful!

  26. I’m in awe of the way you handle topics with both grace and authority.

  27. Thank you for consistently producing such high-quality content.

  28. Such a well-researched piece! It’s evident how much effort you’ve put in.

  29. I admire the way you tackled this complex issue. Very enlightening!

  30. Your work is truly inspirational. I appreciate the depth you bring to your topics.

  31. This article is a perfect blend of informative and entertaining. Well done!

  32. Your ability to distill complex concepts into enjoy readingable content is admirable.

  33. I’m so grateful for the information you’ve shared. It’s been incredibly enlightening!

  34. I’m bookmarking this for future reference. Your advice is spot on!

  35. I’m impressed by your ability to convey such nuanced ideas with clarity.

  36. Your passion for this subject shines through your words. Inspiring!

  37. What a refreshing take on this subject. I completely agree with your points!

  38. I find myself lost in The words, much like one would get lost in someone’s eyes. Lead the way, I’m following.

  39. You have a unique perspective that I find incredibly valuable. Thank you for sharing.

  40. I learned a lot, and now I’m curious about what else you could teach me. The intelligence is as captivating as The prose.

  41. Your creativity and intelligence shine through this post. Amazing job!

  42. This article was a delightful enjoy reading. Your passion is clearly visible!

  43. This post is a testament to your expertise and hard work. Thank you!

  44. Your post was a beacon of knowledge. Thank you for illuminating this subject.

  45. Incredibly informative post! I learned a lot and look forward to more.

  46. I as well conceive hence, perfectly composed post! .

  47. Thank you for the hard work you put into this post. It’s much appreciated!

  48. The Writing is a treasure trove of knowledge, like finding an untouched library book. A rare gem!

  49. Every post of Thes is a learning opportunity for me. Thanks for sharing The expertise.

  50. I appreciate the unique viewpoints you bring to The writing. Very insightful!

Leave a Reply

Your email address will not be published. Required fields are marked *