|
| 1 | +package org.baeldung.hamcrest; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.contains; |
| 4 | +import static org.hamcrest.Matchers.empty; |
| 5 | +import static org.hamcrest.Matchers.emptyArray; |
| 6 | +import static org.hamcrest.Matchers.equalTo; |
| 7 | +import static org.hamcrest.Matchers.hasItem; |
| 8 | +import static org.hamcrest.Matchers.hasItems; |
| 9 | +import static org.hamcrest.Matchers.not; |
| 10 | +import static org.junit.Assert.assertThat; |
| 11 | + |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import com.google.common.collect.Lists; |
| 19 | +import com.google.common.collect.Maps; |
| 20 | + |
| 21 | +public class HamcrestExamplesTest { |
| 22 | + |
| 23 | + // tests |
| 24 | + |
| 25 | + @Test |
| 26 | + public final void whenVerifyingSingleElementIsPartOfCollection_thenCorrect() { |
| 27 | + final List<String> collection = Lists.newArrayList("ab", "cd", "ef"); |
| 28 | + assertThat(collection, hasItem("cd")); |
| 29 | + assertThat(collection, not(hasItem("zz"))); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1() { |
| 34 | + final List<String> collection = Lists.newArrayList("ab", "cd", "ef"); |
| 35 | + assertThat(collection, hasItems("cd", "ef")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect2() { |
| 40 | + final List<String> collection = Lists.newArrayList("ab", "cd", "ef"); |
| 41 | + assertThat(collection, contains("ab", "cd", "ef")); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public final void givenCollectionIsEmpty_whenChecking_thenEmpty() { |
| 46 | + final List<String> collection = Lists.newArrayList(); |
| 47 | + assertThat(collection, empty()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public final void givenCollectionIsNotEmpty_whenChecking_thenNotEmpty() { |
| 52 | + final List<String> collection = Lists.newArrayList("a"); |
| 53 | + assertThat(collection, not(empty())); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public final void givenMapIsEmpty_whenChecking_thenEmpty() { |
| 58 | + final Map<String, String> collection = Maps.newHashMap(); |
| 59 | + assertThat(collection, equalTo(Collections.EMPTY_MAP)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public final void givenArrayIsEmpty_whenChecking_thenEmpty() { |
| 64 | + final String[] array = new String[] { "ab" }; |
| 65 | + assertThat(array, not(emptyArray())); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments