11package com.baeldung.map
22
3- import static org.junit.Assert.*
3+ import com.baeldung.Person
44import org.junit.Test
55
6+ import static org.junit.Assert.*
7+
68class MapUnitTest {
79
10+ private final personMap = [
11+ Regina : new Person (" Regina" , " Fitzpatrick" , 25 ),
12+ Abagail : new Person (" Abagail" , " Ballard" , 26 ),
13+ Lucian : new Person (" Lucian" , " Walter" , 30 )
14+ ]
15+
816 @Test
917 void whenUsingEach_thenMapIsIterated () {
1018 def map = [
@@ -63,7 +71,7 @@ class MapUnitTest {
6371 ' FF6347' : ' Tomato' ,
6472 ' FF4500' : ' Orange Red'
6573 ]
66-
74+
6775 map. eachWithIndex { key , val , index ->
6876 def indent = ((index == 0 || index % 2 == 0 ) ? " " : " " )
6977 println " $indent Hex Code: $key = Color Name: $val "
@@ -82,4 +90,65 @@ class MapUnitTest {
8290 println " Hex Code: $entry . key = Color Name: $entry . value "
8391 }
8492 }
93+
94+ @Test
95+ void whenMapContainsKeyElement_thenCheckReturnsTrue () {
96+ def map = [a : ' d' , b : ' e' , c : ' f' ]
97+
98+ assertTrue (map. containsKey(' a' ))
99+ assertFalse (map. containsKey(' e' ))
100+ assertTrue (map. containsValue(' e' ))
101+ }
102+
103+ @Test
104+ void whenMapContainsKeyElement_thenCheckByMembershipReturnsTrue () {
105+ def map = [a : ' d' , b : ' e' , c : ' f' ]
106+
107+ assertTrue (' a' in map)
108+ assertFalse (' f' in map)
109+ }
110+
111+ @Test
112+ void whenMapContainsFalseBooleanValues_thenCheckReturnsFalse () {
113+ def map = [a : true , b : false , c : null ]
114+
115+ assertTrue (map. containsKey(' b' ))
116+ assertTrue (' a' in map)
117+ assertFalse (' b' in map)
118+ assertFalse (' c' in map)
119+ }
120+
121+ @Test
122+ void givenMapOfPerson_whenUsingStreamMatching_thenShouldEvaluateMap () {
123+ assertTrue (personMap. keySet(). stream(). anyMatch {it == " Regina" })
124+ assertFalse (personMap. keySet(). stream(). allMatch {it == " Albert" })
125+ assertFalse (personMap. values(). stream(). allMatch {it. age < 30 })
126+ assertTrue (personMap. entrySet(). stream(). anyMatch {it. key == " Abagail" && it. value. lastname == " Ballard" })
127+ }
128+
129+ @Test
130+ void givenMapOfPerson_whenUsingCollectionMatching_thenShouldEvaluateMap () {
131+ assertTrue (personMap. keySet(). any {it == " Regina" })
132+ assertFalse (personMap. keySet(). every {it == " Albert" })
133+ assertFalse (personMap. values(). every {it. age < 30 })
134+ assertTrue (personMap. any {firstname , person -> firstname == " Abagail" && person. lastname == " Ballard" })
135+ }
136+
137+ @Test
138+ void givenMapOfPerson_whenUsingCollectionFind_thenShouldReturnElements () {
139+ assertNotNull (personMap. find {it. key == " Abagail" && it. value. lastname == " Ballard" })
140+ assertTrue (personMap. findAll {it. value. age > 20 }. size() == 3 )
141+ }
142+
143+ @Test
144+ void givenMapOfPerson_whenUsingStreamFind_thenShouldReturnElements () {
145+ assertTrue (
146+ personMap. entrySet(). stream()
147+ .filter {it. key == " Abagail" && it. value. lastname == " Ballard" }
148+ .findAny(). isPresent())
149+ assertTrue (
150+ personMap. entrySet(). stream()
151+ .filter {it. value. age > 20 }
152+ .findAll(). size() == 3 )
153+ }
85154}
0 commit comments