From e166f5d9c9777e07dbc9279cf6bb6914629b66ef Mon Sep 17 00:00:00 2001 From: Harsh Jain Date: Fri, 11 May 2018 08:40:46 +0530 Subject: [PATCH] BAEL - 1577 hamcrest core matchers 2.0 version examples as test cases (#4214) * BAEL-1577 : hamcrest core matchers examples added as test cases * BAEL-1577 : Hamcrest java library 2.0.0.0 examples updated as test cases * BAEL-1577 : changed test class name to HamcrestCoreMatchersTest to resolve conflicts --- guava/pom.xml | 9 + .../hamcrest/HamcrestCoreMatchersTest.java | 259 ++++++++++++++++++ 2 files changed, 268 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/hamcrest/HamcrestCoreMatchersTest.java diff --git a/guava/pom.xml b/guava/pom.xml index da880cc995..b36506f87a 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -32,6 +32,14 @@ ${assertj.version} test + + + + org.hamcrest + java-hamcrest + ${java-hamcrest.version} + test + @@ -52,6 +60,7 @@ 3.6.1 + 2.0.0.0 \ No newline at end of file diff --git a/guava/src/test/java/org/baeldung/hamcrest/HamcrestCoreMatchersTest.java b/guava/src/test/java/org/baeldung/hamcrest/HamcrestCoreMatchersTest.java new file mode 100644 index 0000000000..8f3e96c956 --- /dev/null +++ b/guava/src/test/java/org/baeldung/hamcrest/HamcrestCoreMatchersTest.java @@ -0,0 +1,259 @@ +package org.baeldung.hamcrest; + + +import com.google.common.collect.Lists; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.StringEndsWith.endsWith; +import static org.hamcrest.core.StringStartsWith.startsWith; + +public class HamcrestCoreMatchersTest { + + @Test + public void givenTestInput_WhenUsingIsForMatch() { + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, is("hamcrest core")); + assertThat(testString, is(equalTo("hamcrest core"))); + } + + @Test + public void givenDifferentStaticTypeTestInput_WhenUsingEqualToObject_ThenCorrect() { + + // GIVEN + Object original = 100; + + // ASSERT + assertThat(original, equalToObject(100)); + } + + @Test + public void givenTestInput_WhenUsingInstanceOfForClassTypeCheck() { + + assertThat("hamcrest", is(instanceOf(String.class))); + } + + @Test + public void givenTestInput_WhenUsingIsA_ThenAssertType() { + + assertThat("hamcrest core", isA(String.class)); + } + + @Test + public void givenTestInput_WhenUsingEqualToMatcherForEquality() { + + // GIVEN + String actualString = "Hamcrest Core"; + List actualList = Lists.newArrayList("hamcrest", "core"); + + // ASSERT + assertThat(actualString, is(equalTo("Hamcrest Core"))); + assertThat(actualList, is(equalTo(Lists.newArrayList("hamcrest", "core")))); + } + + @Test + public void givenTestInput_WhenUsingNotForMatch() { + + // GIVEN + String testString = "hamcrest"; + + // ASSERT + assertThat(testString, not("hamcrest core")); + assertThat(testString, is(not(equalTo("hamcrest core")))); + assertThat(testString, is(not(instanceOf(Integer.class)))); + } + + @Test + public void givenTestInput_WhenUsingNullValueForNullCheck() { + + // GIVEN + Integer nullObject = null; + + // ASSERT + assertThat(nullObject, is(nullValue())); + assertThat(nullObject, is(nullValue(Integer.class))); + } + + @Test + public void givenTestInput_WhenUsingNotNullValueForNotNullCheck() { + + // GIVEN + Integer testNumber = 123; + + // ASSERT + assertThat(testNumber, is(notNullValue())); + assertThat(testNumber, is(notNullValue(Integer.class))); + } + + @Test + public void givenString_WhenStartsWith_ThenCorrect() { + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, startsWith("hamcrest")); + } + + @Test + public void giveString_WhenStartsWithIgnoringCase_ThenCorrect() { + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, startsWithIgnoringCase("HAMCREST")); + } + + @Test + public void givenString_WhenEndsWith_ThenCorrect() { + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, endsWith("core")); + } + + @Test + public void givenString_WhenEndsWithIgnoringCase_ThenCorrect() { + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, endsWithIgnoringCase("CORE")); + } + + @Test + public void givenString_WhenContainsString_ThenCorrect() { + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, containsString("co")); + } + + @Test + public void givenString_WhenContainsStringIgnoringCase_ThenCorrect() { + + + // GIVEN + String testString = "hamcrest core"; + + // ASSERT + assertThat(testString, containsStringIgnoringCase("CO")); + } + + @Test + public void givenTestInput_WhenUsingHasItemInCollection() { + + // GIVEN + List list = Lists.newArrayList("java", "spring", "baeldung"); + + // ASSERT + assertThat(list, hasItem("java")); + assertThat(list, hasItem(isA(String.class))); + } + + + @Test + public void givenTestInput_WhenUsingHasItemsInCollection() { + + // GIVEN + List list = Lists.newArrayList("java", "spring", "baeldung"); + + // ASSERT + assertThat(list, hasItems("java", "baeldung")); + assertThat(list, hasItems(isA(String.class), endsWith("ing"))); + } + + @Test + public void givenTestInput_WhenUsingAnyForClassType() { + + assertThat("hamcrest", is(any(String.class))); + assertThat("hamcrest", is(any(Object.class))); + } + + @Test + public void givenTestInput_WhenUsingAllOfForAllMatchers() { + + // GIVEN + String testString = "Hamcrest Core"; + + // ASSERT + assertThat(testString, allOf(startsWith("Ham"), endsWith("ore"), containsString("Core"))); + } + + @Test + public void givenTestInput_WhenUsingAnyOfForAnyMatcher() { + + // GIVEN + String testString = "Hamcrest Core"; + + // ASSERT + assertThat(testString, anyOf(startsWith("Ham"), containsString("baeldung"))); + } + + @Test + public void givenTestInput_WhenUsingBothForMatcher() { + + // GIVEN + String testString = "Hamcrest Core Matchers"; + + // ASSERT + assertThat(testString, both(startsWith("Ham")).and(containsString("Core"))); + } + + @Test + public void givenTestInput_WhenUsingEitherForMatcher() { + + // GIVEN + String testString = "Hamcrest Core Matchers"; + + // ASSERT + assertThat(testString, either(startsWith("Bael")).or(containsString("Core"))); + } + + + @Test + public void givenTestInput_WhenUsingEveryItemForMatchInCollection() { + + // GIVEN + List testItems = Lists.newArrayList("Common", "Core", "Combinable"); + + // ASSERT + assertThat(testItems, everyItem(startsWith("Co"))); + } + + @Test + public void givenTwoTestInputs_WhenUsingSameInstanceForMatch() { + + // GIVEN + String string1 = "hamcrest"; + String string2 = string1; + + // ASSERT + assertThat(string1, is(sameInstance(string2))); + } + + @Test + public void givenTwoTestInputs_WhenUsingTheInstanceForMatch() { + // GIVEN + String string1 = "hamcrest"; + String string2 = string1; + + // ASSERT + assertThat(string1, is(theInstance(string2))); + } + +} +