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.

129 Comments:

  1. Reading this gave me a lot of insights. The expertise really shines through, and I’m grateful for it.

  2. The clarity and thoughtfulness of The approach is as appealing as a deep conversation over coffee.

  3. Bookmarking this for future reference, but also because The advice is as invaluable as The attention.

  4. I was truly impressed by how deeply you delved into this topic. The hard work hasn’t gone unnoticed!

  5. The posts are like a cozy nook, inviting and comfortable, where I can immerse myself in thoughts.

  6. The consistency and high quality of The content are something I really appreciate. Thank you for The dedication.

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

  8. The content is like a treasure chest; every post uncovers gems of wisdom. X marks the spot here.

  9. I’m bookmarking this for future reference. The advice is spot on!

  10. The Writing has become like a favorite meeting spot, where great minds and ideas mingle.

  11. Brilliant writing! You’ve perfectly captured the essence of the topic.

  12. Beautifully written and informative, making the rest of the internet look bad.

  13. The creativity and insight left a big impression on me. Fantastic job!

  14. The writing style had me at hello. Engaged from start to finish, just like a perfect first date.

  15. The breadth of The knowledge is amazing. Thanks for sharing The insights with us.

  16. The depth you bring to The topics is like diving into a deep pool, refreshing and invigorating.

  17. Incredibly helpful post, like a GPS for my lost thoughts.

  18. Tackled this hard to understand issue with elegance. I didn’t know we were at a ballet.

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

  20. Incredibly helpful post, like a GPS for my lost thoughts.

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

  22. This post is a testament not only to the expertise on the state of the country but also to the dedication. Truly inspiring.

  23. The insights added a lot of value, in a way only Google Scholar dreams of. Thanks for the enlightenment.

  24. I’m bookmarking this for future reference. The advice on the state of the country is spot on!

  25. I appreciate how you’ve explained things so clearly. It really helped me understand the topic better.

  26. The depth of The research is impressive, almost as much as the way you make hard to understand topics captivating.

  27. The ability to connect with readers is like a secret handshake, making us feel part of an exclusive club.

  28. Incredibly informative post on the state of the country! I learned a lot and look forward to more.

  29. The article was a delightful read. It’s clear you’re passionate about what you do, and it shows.

Leave a Reply

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