|
| 1 | +package org.baeldung.hamcrest; |
| 2 | + |
| 3 | +import static org.baeldung.hamcrest.IsPositiveInteger.isAPositiveInteger; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.Matchers.equalTo; |
| 6 | +import static org.hamcrest.Matchers.greaterThan; |
| 7 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 8 | +import static org.hamcrest.Matchers.lessThan; |
| 9 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 10 | +import static org.hamcrest.Matchers.closeTo; |
| 11 | +import static org.hamcrest.beans.HasProperty.hasProperty; |
| 12 | +import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; |
| 13 | +import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs; |
| 14 | +import static org.hamcrest.collection.IsArrayContaining.hasItemInArray; |
| 15 | +import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder; |
| 16 | +import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; |
| 17 | +import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; |
| 18 | +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
| 19 | +import static org.hamcrest.collection.IsEmptyCollection.empty; |
| 20 | +import static org.hamcrest.collection.IsIn.isIn; |
| 21 | +import static org.hamcrest.collection.IsIn.isOneOf; |
| 22 | +import static org.hamcrest.collection.IsMapContaining.hasKey; |
| 23 | +import static org.hamcrest.collection.IsMapContaining.hasEntry; |
| 24 | +import static org.hamcrest.collection.IsMapContaining.hasValue; |
| 25 | +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; |
| 26 | +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; |
| 27 | +import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase; |
| 28 | +import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString; |
| 29 | +import static org.hamcrest.text.IsEmptyString.isEmptyString; |
| 30 | +import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace; |
| 31 | +import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder; |
| 32 | +import static org.hamcrest.object.HasToString.hasToString; |
| 33 | +import static org.hamcrest.object.IsCompatibleType.typeCompatibleWith; |
| 34 | +import static org.hamcrest.core.AllOf.allOf; |
| 35 | +import static org.hamcrest.core.AnyOf.anyOf; |
| 36 | +import static org.hamcrest.core.Every.everyItem; |
| 37 | +import static org.hamcrest.core.Is.is;//class |
| 38 | +import static org.hamcrest.core.IsInstanceOf.instanceOf; |
| 39 | +import static org.hamcrest.core.IsNot.not;//matcher |
| 40 | +import static org.hamcrest.core.IsNull.notNullValue;//without arg |
| 41 | +import static org.hamcrest.core.IsSame.sameInstance; |
| 42 | +import static org.hamcrest.core.StringContains.containsString; |
| 43 | +import static org.hamcrest.core.StringEndsWith.endsWith; |
| 44 | +import static org.hamcrest.core.StringStartsWith.startsWith; |
| 45 | + |
| 46 | +import java.util.ArrayList; |
| 47 | +import java.util.Arrays; |
| 48 | +import java.util.HashMap; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Map; |
| 51 | + |
| 52 | +import org.junit.Test; |
| 53 | + |
| 54 | +public class HamcrestMatcherTest { |
| 55 | + @Test |
| 56 | + public void given2Strings_whenEqual_thenCorrect() { |
| 57 | + String a = "foo"; |
| 58 | + String b = "FOO"; |
| 59 | + assertThat(a, equalToIgnoringCase(b)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenBean_whenHasValue_thenCorrect() { |
| 64 | + Person person = new Person("Baeldung", "New York"); |
| 65 | + assertThat(person, hasProperty("name")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenBean_whenHasCorrectValue_thenCorrect() { |
| 70 | + Person person = new Person("Baeldung", "New York"); |
| 71 | + assertThat(person, hasProperty("address", equalTo("New York"))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void given2Beans_whenHavingSameValues_thenCorrect() { |
| 76 | + Person person1 = new Person("Baeldung", "New York"); |
| 77 | + Person person2 = new Person("Baeldung", "New York"); |
| 78 | + assertThat(person1, samePropertyValuesAs(person2)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenAList_whenChecksSize_thenCorrect() { |
| 83 | + List<String> hamcrestMatchers = Arrays.asList("collections", "beans", |
| 84 | + "text", "number"); |
| 85 | + assertThat(hamcrestMatchers, hasSize(4)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void givenArray_whenChecksSize_thenCorrect() { |
| 90 | + String[] hamcrestMatchers = { "collections", "beans", "text", "number" }; |
| 91 | + assertThat(hamcrestMatchers, arrayWithSize(4)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void givenAListAndValues_whenChecksListForGivenValues_thenCorrect() { |
| 96 | + List<String> hamcrestMatchers = Arrays.asList("collections", "beans", |
| 97 | + "text", "number"); |
| 98 | + assertThat(hamcrestMatchers, |
| 99 | + containsInAnyOrder("beans", "text", "collections", "number")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void givenAListAndValues_whenChecksListForGivenValuesWithOrder_thenCorrect() { |
| 104 | + List<String> hamcrestMatchers = Arrays.asList("collections", "beans", |
| 105 | + "text", "number"); |
| 106 | + assertThat(hamcrestMatchers, |
| 107 | + contains("collections", "beans", "text", "number")); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void givenArrayAndValue_whenValueFoundInArray_thenCorrect() { |
| 112 | + String[] hamcrestMatchers = { "collections", "beans", "text", "number" }; |
| 113 | + assertThat(hamcrestMatchers, hasItemInArray("text")); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void givenValueAndArray_whenValueIsOneOfArrayElements_thenCorrect() { |
| 119 | + String[] hamcrestMatchers = { "collections", "beans", "text", "number" }; |
| 120 | + assertThat("text", isOneOf(hamcrestMatchers)); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void givenArrayAndValues_whenValuesFoundInArray_thenCorrect() { |
| 126 | + String[] hamcrestMatchers = { "collections", "beans", "text", "number" }; |
| 127 | + assertThat( |
| 128 | + hamcrestMatchers, |
| 129 | + arrayContainingInAnyOrder("beans", "collections", "number", |
| 130 | + "text")); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void givenArrayAndValues_whenValuesFoundInArrayInOrder_thenCorrect() { |
| 136 | + String[] hamcrestMatchers = { "collections", "beans", "text", "number" }; |
| 137 | + assertThat(hamcrestMatchers, |
| 138 | + arrayContaining("collections", "beans", "text", "number")); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void givenCollection_whenEmpty_thenCorrect() { |
| 144 | + List<String> emptyList = new ArrayList<>(); |
| 145 | + assertThat(emptyList, empty()); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void givenValueAndArray_whenValueFoundInArray_thenCorrect() { |
| 151 | + String[] array = new String[] { "collections", "beans", "text", |
| 152 | + "number" }; |
| 153 | + assertThat("bean", isIn(array)); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void givenMapAndKey_whenKeyFoundInMap_thenCorrect() { |
| 159 | + Map<String, String> map = new HashMap<>(); |
| 160 | + map.put("blogname", "baeldung"); |
| 161 | + assertThat(map, hasKey("blogname")); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void givenMapAndEntry_whenEntryFoundInMap_thenCorrect() { |
| 166 | + Map<String, String> map = new HashMap<>(); |
| 167 | + map.put("blogname", "baeldung"); |
| 168 | + assertThat(map, hasEntry("blogname", "baeldung")); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void givenMapAndValue_whenValueFoundInMap_thenCorrect() { |
| 173 | + Map<String, String> map = new HashMap<>(); |
| 174 | + map.put("blogname", "baeldung"); |
| 175 | + assertThat(map, hasValue("baeldung")); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void givenString_whenEmpty_thenCorrect() { |
| 180 | + String str = ""; |
| 181 | + assertThat(str, isEmptyString()); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void givenString_whenEmptyOrNull_thenCorrect() { |
| 186 | + String str = null; |
| 187 | + assertThat(str, isEmptyOrNullString()); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void given2Strings_whenEqualRegardlessWhiteSpace_thenCorrect() { |
| 192 | + String str1 = "text"; |
| 193 | + String str2 = " text "; |
| 194 | + assertThat(str1, equalToIgnoringWhiteSpace(str2)); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void givenString_whenContainsGivenSubstring_thenCorrect() { |
| 199 | + String str = "calligraphy"; |
| 200 | + assertThat(str, stringContainsInOrder(Arrays.asList("call", "graph"))); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void givenBean_whenToStringReturnsRequiredString_thenCorrect() { |
| 205 | + Person person = new Person("Barrack", "Washington"); |
| 206 | + String str = person.toString(); |
| 207 | + assertThat(person, hasToString(str)); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void given2Classes_whenOneInheritsFromOther_thenCorrect() { |
| 212 | + assertThat(Cat.class, typeCompatibleWith(Animal.class)); |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + @Test |
| 217 | + public void given2Strings_whenIsEqualRegardlessWhiteSpace_thenCorrect() { |
| 218 | + String str1 = "text"; |
| 219 | + String str2 = " text "; |
| 220 | + assertThat(str1, is(equalToIgnoringWhiteSpace(str2))); |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + public void given2Strings_whenIsNotEqualRegardlessWhiteSpace_thenCorrect() { |
| 225 | + String str1 = "text"; |
| 226 | + String str2 = " texts "; |
| 227 | + assertThat(str1, not(equalToIgnoringWhiteSpace(str2))); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void given2Strings_whenNotEqual_thenCorrect() { |
| 232 | + String str1 = "text"; |
| 233 | + String str2 = "texts"; |
| 234 | + assertThat(str1, not(str2)); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + public void given2Strings_whenIsEqual_thenCorrect() { |
| 239 | + String str1 = "text"; |
| 240 | + String str2 = "text"; |
| 241 | + assertThat(str1, is(str2)); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void givenAStrings_whenContainsAnotherGivenString_thenCorrect() { |
| 246 | + String str1 = "calligraphy"; |
| 247 | + String str2 = "call"; |
| 248 | + assertThat(str1, containsString(str2)); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void givenAString_whenEndsWithAnotherGivenString_thenCorrect() { |
| 253 | + String str1 = "calligraphy"; |
| 254 | + String str2 = "phy"; |
| 255 | + assertThat(str1, endsWith(str2)); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + public void givenAString_whenStartsWithAnotherGivenString_thenCorrect() { |
| 260 | + String str1 = "calligraphy"; |
| 261 | + String str2 = "call"; |
| 262 | + assertThat(str1, startsWith(str2)); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + public void given2Objects_whenSameInstance_thenCorrect() { |
| 267 | + Cat cat = new Cat(); |
| 268 | + assertThat(cat, sameInstance(cat)); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void givenAnObject_whenInstanceOfGivenClass_thenCorrect() { |
| 273 | + Cat cat = new Cat(); |
| 274 | + assertThat(cat, instanceOf(Cat.class)); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + public void givenList_whenEachElementGreaterThan0_thenCorrect() { |
| 279 | + List<Integer> list = Arrays.asList(1, 2, 3); |
| 280 | + int baseCase = 0; |
| 281 | + assertThat(list, everyItem(greaterThan(baseCase))); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void givenString_whenNotNull_thenCorrect() { |
| 286 | + String str = "notnull"; |
| 287 | + assertThat(str, notNullValue()); |
| 288 | + } |
| 289 | + |
| 290 | + @Test |
| 291 | + public void givenString_whenMeetsAnyOfGivenConditions_thenCorrect() { |
| 292 | + String str = "calligraphy"; |
| 293 | + String start = "call"; |
| 294 | + String end = "foo"; |
| 295 | + assertThat(str, anyOf(startsWith(start), containsString(end))); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + public void givenString_whenMeetsAllOfGivenConditions_thenCorrect() { |
| 300 | + String str = "calligraphy"; |
| 301 | + String start = "call"; |
| 302 | + String end = "foo"; |
| 303 | + assertThat(str, allOf(startsWith(start), endsWith(end))); |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void givenInteger_whenAPositiveValue_thenCorrect() { |
| 308 | + int num = 1; |
| 309 | + assertThat(num, isAPositiveInteger()); |
| 310 | + } |
| 311 | + |
| 312 | + @Test |
| 313 | + public void givenAnInteger_whenGreaterThan0_thenCorrect() { |
| 314 | + int num = 1; |
| 315 | + assertThat(num, greaterThan(0)); |
| 316 | + } |
| 317 | + |
| 318 | + @Test |
| 319 | + public void givenAnInteger_whenGreaterThanOrEqTo5_thenCorrect() { |
| 320 | + int num = 5; |
| 321 | + assertThat(num, greaterThanOrEqualTo(5)); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + public void givenAnInteger_whenLessThan0_thenCorrect() { |
| 326 | + int num = -1; |
| 327 | + assertThat(num, lessThan(0)); |
| 328 | + } |
| 329 | + |
| 330 | + @Test |
| 331 | + public void givenAnInteger_whenLessThanOrEqTo5_thenCorrect() { |
| 332 | + assertThat(-1, lessThanOrEqualTo(5)); |
| 333 | + } |
| 334 | + |
| 335 | + @Test |
| 336 | + public void givenADouble_whenCloseTo_thenCorrect() { |
| 337 | + assertThat(1.2, closeTo(1, 0.5)); |
| 338 | + } |
| 339 | + |
| 340 | +} |
0 commit comments