|
| 1 | +package org.baeldung.hamcrest; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.collect.Lists; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import static org.hamcrest.CoreMatchers.*; |
| 10 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | +import static org.hamcrest.core.StringEndsWith.endsWith; |
| 12 | +import static org.hamcrest.core.StringStartsWith.startsWith; |
| 13 | + |
| 14 | +public class HamcrestCoreMatchersTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void givenTestInput_WhenUsingIsForMatch() { |
| 18 | + |
| 19 | + // GIVEN |
| 20 | + String testString = "hamcrest core"; |
| 21 | + |
| 22 | + // ASSERT |
| 23 | + assertThat(testString, is("hamcrest core")); |
| 24 | + assertThat(testString, is(equalTo("hamcrest core"))); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void givenDifferentStaticTypeTestInput_WhenUsingEqualToObject_ThenCorrect() { |
| 29 | + |
| 30 | + // GIVEN |
| 31 | + Object original = 100; |
| 32 | + |
| 33 | + // ASSERT |
| 34 | + assertThat(original, equalToObject(100)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenTestInput_WhenUsingInstanceOfForClassTypeCheck() { |
| 39 | + |
| 40 | + assertThat("hamcrest", is(instanceOf(String.class))); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenTestInput_WhenUsingIsA_ThenAssertType() { |
| 45 | + |
| 46 | + assertThat("hamcrest core", isA(String.class)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenTestInput_WhenUsingEqualToMatcherForEquality() { |
| 51 | + |
| 52 | + // GIVEN |
| 53 | + String actualString = "Hamcrest Core"; |
| 54 | + List<String> actualList = Lists.newArrayList("hamcrest", "core"); |
| 55 | + |
| 56 | + // ASSERT |
| 57 | + assertThat(actualString, is(equalTo("Hamcrest Core"))); |
| 58 | + assertThat(actualList, is(equalTo(Lists.newArrayList("hamcrest", "core")))); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void givenTestInput_WhenUsingNotForMatch() { |
| 63 | + |
| 64 | + // GIVEN |
| 65 | + String testString = "hamcrest"; |
| 66 | + |
| 67 | + // ASSERT |
| 68 | + assertThat(testString, not("hamcrest core")); |
| 69 | + assertThat(testString, is(not(equalTo("hamcrest core")))); |
| 70 | + assertThat(testString, is(not(instanceOf(Integer.class)))); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void givenTestInput_WhenUsingNullValueForNullCheck() { |
| 75 | + |
| 76 | + // GIVEN |
| 77 | + Integer nullObject = null; |
| 78 | + |
| 79 | + // ASSERT |
| 80 | + assertThat(nullObject, is(nullValue())); |
| 81 | + assertThat(nullObject, is(nullValue(Integer.class))); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void givenTestInput_WhenUsingNotNullValueForNotNullCheck() { |
| 86 | + |
| 87 | + // GIVEN |
| 88 | + Integer testNumber = 123; |
| 89 | + |
| 90 | + // ASSERT |
| 91 | + assertThat(testNumber, is(notNullValue())); |
| 92 | + assertThat(testNumber, is(notNullValue(Integer.class))); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void givenString_WhenStartsWith_ThenCorrect() { |
| 97 | + |
| 98 | + // GIVEN |
| 99 | + String testString = "hamcrest core"; |
| 100 | + |
| 101 | + // ASSERT |
| 102 | + assertThat(testString, startsWith("hamcrest")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void giveString_WhenStartsWithIgnoringCase_ThenCorrect() { |
| 107 | + |
| 108 | + // GIVEN |
| 109 | + String testString = "hamcrest core"; |
| 110 | + |
| 111 | + // ASSERT |
| 112 | + assertThat(testString, startsWithIgnoringCase("HAMCREST")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void givenString_WhenEndsWith_ThenCorrect() { |
| 117 | + |
| 118 | + // GIVEN |
| 119 | + String testString = "hamcrest core"; |
| 120 | + |
| 121 | + // ASSERT |
| 122 | + assertThat(testString, endsWith("core")); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void givenString_WhenEndsWithIgnoringCase_ThenCorrect() { |
| 127 | + |
| 128 | + // GIVEN |
| 129 | + String testString = "hamcrest core"; |
| 130 | + |
| 131 | + // ASSERT |
| 132 | + assertThat(testString, endsWithIgnoringCase("CORE")); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void givenString_WhenContainsString_ThenCorrect() { |
| 137 | + |
| 138 | + // GIVEN |
| 139 | + String testString = "hamcrest core"; |
| 140 | + |
| 141 | + // ASSERT |
| 142 | + assertThat(testString, containsString("co")); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void givenString_WhenContainsStringIgnoringCase_ThenCorrect() { |
| 147 | + |
| 148 | + |
| 149 | + // GIVEN |
| 150 | + String testString = "hamcrest core"; |
| 151 | + |
| 152 | + // ASSERT |
| 153 | + assertThat(testString, containsStringIgnoringCase("CO")); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void givenTestInput_WhenUsingHasItemInCollection() { |
| 158 | + |
| 159 | + // GIVEN |
| 160 | + List<String> list = Lists.newArrayList("java", "spring", "baeldung"); |
| 161 | + |
| 162 | + // ASSERT |
| 163 | + assertThat(list, hasItem("java")); |
| 164 | + assertThat(list, hasItem(isA(String.class))); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + @Test |
| 169 | + public void givenTestInput_WhenUsingHasItemsInCollection() { |
| 170 | + |
| 171 | + // GIVEN |
| 172 | + List<String> list = Lists.newArrayList("java", "spring", "baeldung"); |
| 173 | + |
| 174 | + // ASSERT |
| 175 | + assertThat(list, hasItems("java", "baeldung")); |
| 176 | + assertThat(list, hasItems(isA(String.class), endsWith("ing"))); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void givenTestInput_WhenUsingAnyForClassType() { |
| 181 | + |
| 182 | + assertThat("hamcrest", is(any(String.class))); |
| 183 | + assertThat("hamcrest", is(any(Object.class))); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void givenTestInput_WhenUsingAllOfForAllMatchers() { |
| 188 | + |
| 189 | + // GIVEN |
| 190 | + String testString = "Hamcrest Core"; |
| 191 | + |
| 192 | + // ASSERT |
| 193 | + assertThat(testString, allOf(startsWith("Ham"), endsWith("ore"), containsString("Core"))); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void givenTestInput_WhenUsingAnyOfForAnyMatcher() { |
| 198 | + |
| 199 | + // GIVEN |
| 200 | + String testString = "Hamcrest Core"; |
| 201 | + |
| 202 | + // ASSERT |
| 203 | + assertThat(testString, anyOf(startsWith("Ham"), containsString("baeldung"))); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void givenTestInput_WhenUsingBothForMatcher() { |
| 208 | + |
| 209 | + // GIVEN |
| 210 | + String testString = "Hamcrest Core Matchers"; |
| 211 | + |
| 212 | + // ASSERT |
| 213 | + assertThat(testString, both(startsWith("Ham")).and(containsString("Core"))); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void givenTestInput_WhenUsingEitherForMatcher() { |
| 218 | + |
| 219 | + // GIVEN |
| 220 | + String testString = "Hamcrest Core Matchers"; |
| 221 | + |
| 222 | + // ASSERT |
| 223 | + assertThat(testString, either(startsWith("Bael")).or(containsString("Core"))); |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + @Test |
| 228 | + public void givenTestInput_WhenUsingEveryItemForMatchInCollection() { |
| 229 | + |
| 230 | + // GIVEN |
| 231 | + List<String> testItems = Lists.newArrayList("Common", "Core", "Combinable"); |
| 232 | + |
| 233 | + // ASSERT |
| 234 | + assertThat(testItems, everyItem(startsWith("Co"))); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + public void givenTwoTestInputs_WhenUsingSameInstanceForMatch() { |
| 239 | + |
| 240 | + // GIVEN |
| 241 | + String string1 = "hamcrest"; |
| 242 | + String string2 = string1; |
| 243 | + |
| 244 | + // ASSERT |
| 245 | + assertThat(string1, is(sameInstance(string2))); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void givenTwoTestInputs_WhenUsingTheInstanceForMatch() { |
| 250 | + // GIVEN |
| 251 | + String string1 = "hamcrest"; |
| 252 | + String string2 = string1; |
| 253 | + |
| 254 | + // ASSERT |
| 255 | + assertThat(string1, is(theInstance(string2))); |
| 256 | + } |
| 257 | + |
| 258 | +} |
| 259 | + |
0 commit comments