Created
April 17, 2016 20:19
-
-
Save vainikkaj/d55f7fdb647bdf5ef0d588ea45170842 to your computer and use it in GitHub Desktop.
Collection of different hamcrest examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util.hamcrest; | |
import static org.hamcrest.CoreMatchers.containsString; | |
import static org.hamcrest.core.AllOf.allOf; | |
import static org.hamcrest.core.AnyOf.anyOf; | |
import static org.hamcrest.core.DescribedAs.describedAs; | |
import static org.hamcrest.core.Is.is; | |
import static org.hamcrest.core.IsAnything.anything; | |
import static org.hamcrest.core.IsEqual.equalTo; | |
import static org.hamcrest.core.IsInstanceOf.any; | |
import static org.hamcrest.core.IsInstanceOf.instanceOf; | |
import static org.hamcrest.core.IsNot.not; | |
import static org.hamcrest.core.IsNull.notNullValue; | |
import static org.hamcrest.core.IsNull.nullValue; | |
import static org.hamcrest.core.IsSame.sameInstance; | |
import static org.junit.Assert.assertThat; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.StringDescription; | |
import org.junit.Test; | |
/** | |
* <p> | |
* A good starting place is the assertThat() method that can now almost always | |
* be used in place of the traditional assertEquals(). assertThat() can be found | |
* in org.<b | |
* style="color: black; background-color: rgb(153, 255, 153);">junit</b>.Assert, | |
* but it defines using <b | |
* style="color: black; background-color: rgb(255, 255, 102);">Hamcrest</b> | |
* matchers in the signature: | |
* </p> | |
* <p> | |
* <b>static <T> void assertThat(T actual, | |
* org.<b>hamcrest</b>.Matcher<T> matcher)</b> | |
* </p> | |
* | |
*/ | |
public class HamcrestMatcherExamplesTest { | |
@Test public void simpleContains() { | |
assertThat("hello", containsString("el")); | |
} | |
@Test public void allOfExampleShowsAllMatchersMustAllBeTrue() | |
throws Exception { | |
assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello")))); | |
} | |
@Test public void anyExampleChecksThatClassIsOfSameType() throws Exception { | |
assertThat("Hello", is(any(String.class))); | |
} | |
@Test public void anyExampleShowsStringIsAlsoAnObject() throws Exception { | |
assertThat("Hello", is(any(Object.class))); | |
} | |
@SuppressWarnings("unchecked") @Test public void anyOfExampleReturnsTrueIfOneMatches() | |
throws Exception { | |
assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye")))); | |
} | |
@SuppressWarnings("unchecked") @Test public void anyOfExampleFailingIfAllMatchersAreFalse() | |
throws Exception { | |
assertThat("Hello", | |
is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye"))))); | |
} | |
@Test public void anythingExampleAlwaysReturnsTrue() throws Exception { | |
assertThat("Hello", is(anything())); | |
} | |
public void describedAsExample() throws Exception { | |
Matcher<?> matcher = describedAs("My Description", anything()); | |
Description description = new StringDescription().appendDescriptionOf(matcher); | |
assertThat("My Description", is(description.toString())); | |
} | |
@Test public void equalToExampleAddingTwoPlusTwo() throws Exception { | |
assertThat(2 + 2, is(equalTo(4))); | |
} | |
@Test public void instanceOfExampleForString() throws Exception { | |
assertThat("Hello", is(instanceOf(String.class))); | |
} | |
@Test public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception { | |
assertThat("Hello", is(is(is(notNullValue())))); | |
} | |
@Test public void isExampleShortCutForIsInstanceOfClass() throws Exception { | |
assertThat("Hello", instanceOf(String.class)); | |
} | |
@Test public void isExampleShortCutForIsEqualTo() throws Exception { | |
assertThat("Hello", is("Hello")); | |
assertThat("Hello", equalTo("Hello")); | |
} | |
@Test public void notExampleJustInvertsExpression() throws Exception { | |
assertThat("Hello", is(not(instanceOf(Integer.class)))); | |
} | |
@Test public void notNullValueExampleForString() throws Exception { | |
assertThat("Hello", is(notNullValue())); | |
} | |
@Test public void notNullValueExampleForAClass() throws Exception { | |
assertThat("Hello", is(notNullValue(Object.class))); | |
} | |
@Test public void nullValueExampleWithANull() throws Exception { | |
assertThat(null, is(nullValue())); | |
} | |
@Test public void nullValueExampleWithANullType() throws Exception { | |
Integer nothing = null; | |
assertThat(nothing, is(nullValue(Integer.class))); | |
} | |
@Test public void sameInstanceExample() throws Exception { | |
Object object = new Object(); | |
Object sameObject = object; | |
assertThat(object, is(sameInstance(sameObject))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment