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 tackle topics with such finesse, it’s like watching a skilled chef at work. Serving up knowledge with flair!

  2. Discovering The Writing has been a game-changer for me. The contributions are invaluable.

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

  4. The argumentation was compelling and well-structured. I found myself nodding along as I read.

  5. The way you break down ideas is like a chef explaining a recipe, making hard to understand dishes seem simple.

  6. A breath of fresh air, or what I needed after being suffocated by mediocrity.

  7. Consistently high-high quality content, as if you’re trying to show us all up.

  8. A refreshing take on the subject, like a cool breeze on a hot day. I’m all ears for what you have to say next.

  9. The Writing is like a secret garden, each post a path leading to new discoveries and delights.

  10. This Writing is a treasure trove of knowledge. Thank you for The contributions!

  11. dolores excepturi occaecati id aliquam impedit quos repellendus officia eaque ea magnam et soluta odio. ipsam ad nesciunt atque incidunt tempore pariatur qui et ut.

  12. magnam nam adipisci ipsa illo itaque mollitia veritatis expedita sunt iusto quia nam minus. sunt quo atque sint quia voluptatum libero corrupti aut consequuntur atque. ut et aliquam quo ut aliquid omn

  13. This was a thoroughly insightful read. Thank you for sharing The expertise!

  14. The fresh insights were a breath of fresh air. Thank you for sharing The unique perspective.

  15. A beacon of knowledge, or so I thought until I realized it’s just The shining confidence.

  16. Reading The work is like gazing at a masterpiece; every detail contributes to a breathtaking whole.

  17. The piece was both informative and thought-provoking, like a deep conversation that lingers into the night.

  18. The insights add so much value to the conversation. I always learn something new from you.

  19. Stumbling upon this article was the highlight of my day, much like catching a glimpse of a smile across the room.

  20. The insights dazzled me more than a candlelit dinner. Thanks for lighting up my intellect.

  21. Each post is a window into The thoughts, and I must say, the view is stunning.

  22. Fantastic job breaking down this topic, like a demolition crew for my misconceptions.

  23. The blend of informative and entertaining content is perfect. I enjoyed every word.

  24. Compelling read with well-presented arguments. I almost felt persuaded. Almost.

  25. The posts are like a secret garden of knowledge. I’m always excited to see what’s blooming.

  26. The work is both informative and thought-provoking. I’m really impressed by the high quality of The content.

  27. A masterpiece of writing—you’ve covered all bases with such finesse, I’m left wanting an encore.

  28. The ability to break down tough concepts is as impressive as a magician’s trick. Color me amazed.

  29. The Writing is like a favorite coffee shop where the drinks are always warm and the atmosphere is inviting.

  30. The elegance of The arguments is as captivating as a sunset. I could admire it all day.

  31. The ability to present nuanced ideas so clearly is something I truly respect.

  32. This post is packed with insights, each one a gentle nudge to my intellect and curiosity.

  33. Provoked thought and taught me something new, as if my brain needed more exercise.

  34. You’ve opened my eyes to new perspectives. Thank you for the enlightenment!

  35. What a compelling read! The arguments were well-presented and convincing.

  36. The Writing is a constant source of inspiration and knowledge for me. I can’t thank you enough.

  37. The insights light up my intellect like fireworks. Thanks for the show!

  38. You write with such passion and clarity, it’s like listening to a love song for the mind.

  39. This post is packed with insights I hadn’t considered before. Thanks for broadening my horizons.

  40. I appreciate the balance and fairness in The writing, like a perfect partner who always keeps things interesting. Great job!

  41. Opened my eyes to new perspectives, and here I was thinking I’d seen it all.

  42. This post is a testament not only to The expertise but also to The dedication. Truly inspiring.

  43. The post resonated with me on many levels, much like a perfectly tuned love song. Thanks for the harmony.

  44. Stumbling upon The article was a highlight of my day. It was just what I needed to read.

  45. Handling topics with grace and authority, like a professor, but without the monotone lectures.

  46. The Writing is a go-to resource for me. Thanks for all the hard work!

  47. Discovering The Writing felt like finding the perfect match. The intellect and charm are a rare combo.

  48. The ability to break down tough concepts is as impressive as a magician’s trick. Color me amazed.

  49. A constant source of inspiration and knowledge, like a muse but less mythical.

Leave a Reply

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