From e2371cec0172a2b876c4327b659b2d2a43277607 Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Mon, 28 Oct 2013 15:30:07 +0200 Subject: [PATCH] testing examples for hamcrest --- .../GuavaCollectionsExamplesTest.java | 2 +- .../GuavaOrderingExamplesTest.java | 2 +- .../hamcrest/HamcrestExamplesTest.java | 68 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) rename guava/src/test/java/org/baeldung/guava/{collections => }/GuavaCollectionsExamplesTest.java (99%) rename guava/src/test/java/org/baeldung/guava/{collections => }/GuavaOrderingExamplesTest.java (99%) create mode 100644 guava/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java diff --git a/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java similarity index 99% rename from guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java rename to guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java index b270653ffa..dc6bfe4c0b 100644 --- a/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java @@ -1,4 +1,4 @@ -package org.baeldung.guava.collections; +package org.baeldung.guava; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; diff --git a/guava/src/test/java/org/baeldung/guava/collections/GuavaOrderingExamplesTest.java b/guava/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java similarity index 99% rename from guava/src/test/java/org/baeldung/guava/collections/GuavaOrderingExamplesTest.java rename to guava/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java index 1a2a6a816b..5821d19ba0 100644 --- a/guava/src/test/java/org/baeldung/guava/collections/GuavaOrderingExamplesTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java @@ -1,4 +1,4 @@ -package org.baeldung.guava.collections; +package org.baeldung.guava; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; diff --git a/guava/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java b/guava/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java new file mode 100644 index 0000000000..99a9209b36 --- /dev/null +++ b/guava/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java @@ -0,0 +1,68 @@ +package org.baeldung.hamcrest; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.emptyArray; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +public class HamcrestExamplesTest { + + // tests + + @Test + public final void whenVerifyingSingleElementIsPartOfCollection_thenCorrect() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, hasItem("cd")); + assertThat(collection, not(hasItem("zz"))); + } + + @Test + public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, hasItems("cd", "ef")); + } + + @Test + public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect2() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, contains("ab", "cd", "ef")); + } + + @Test + public final void givenCollectionIsEmpty_whenChecking_thenEmpty() { + final List collection = Lists.newArrayList(); + assertThat(collection, empty()); + } + + @Test + public final void givenCollectionIsNotEmpty_whenChecking_thenNotEmpty() { + final List collection = Lists.newArrayList("a"); + assertThat(collection, not(empty())); + } + + @Test + public final void givenMapIsEmpty_whenChecking_thenEmpty() { + final Map collection = Maps.newHashMap(); + assertThat(collection, equalTo(Collections.EMPTY_MAP)); + } + + @Test + public final void givenArrayIsEmpty_whenChecking_thenEmpty() { + final String[] array = new String[] { "ab" }; + assertThat(array, not(emptyArray())); + } + +}