Arrays
Array
import static org.hamcrest.Matchers.array;
assertThat(new Integer[]{1,2,3}, array(equalTo(1), equalTo(2), equalTo(3)));
assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))));
ArrayContaining
import static org.hamcrest.Matchers.arrayContaining;
assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"));
// With matcher as parameter
assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")));
// With list of matchers as parameters
List<org.hamcrest.Matcher<? super String>> matchers = new ArrayList<org.hamcrest.Matcher<? super String>>();
matchers.add(equalTo("foo"));
matchers.add(equalTo("bar"));
assertThat(new String[]{"foo", "bar"}, arrayContaining(matchers));
ArrayContainingInAnyOrder
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"));
// With matchers as parameters
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")));
// With list of matchers as parameters
List<org.hamcrest.Matcher<? super String>> matchers = new ArrayList<org.hamcrest.Matcher<? super String>>();
matchers.add(equalTo("foo"));
matchers.add(equalTo("bar"));
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(matchers));
ArrayWithSize
import static org.hamcrest.Matchers.arrayWithSize;
assertThat(new String[]{"foo", "bar"}, arrayWithSize(2));
EmptyArray
import static org.hamcrest.Matchers.emptyArray;
assertThat(new String[0], emptyArray());
EqualTo
import static org.hamcrest.Matchers.equalTo;
assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)));
HasItemInArray
import static org.hamcrest.Matchers.hasItemInArray;
assertThat(new String[] {"foo", "bar"}, hasItemInArray("bar"));
StartsWith
import static org.hamcrest.Matchers.startsWith;
assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")));
Beans
EqualTo
import static org.hamcrest.Matchers.equalTo;
File bean = new File("filename");
assertThat(bean, hasProperty("path", equalTo("filename")));
HasProperty
import static org.hamcrest.Matchers.hasProperty;
File bean = new File("filename");
assertThat(bean, hasProperty("path"));
SamePropertyValuesAs
import static org.hamcrest.Matchers.samePropertyValuesAs;
File bean = new File("filename");
File expectedBean = new File("filename");
assertThat(bean, samePropertyValuesAs(expectedBean));
Classes
TypeCompatibleWith
import static org.hamcrest.Matchers.typeCompatibleWith;
assertThat(Integer.class, typeCompatibleWith(Number.class));
Collections
Empty
import static org.hamcrest.Matchers.empty;
assertThat(new ArrayList<String>(), empty());
assertThat(new ArrayList<String>(), is(empty()));
EmptyCollectionOf
import static org.hamcrest.Matchers.emptyCollectionOf;
assertThat(new ArrayList<String>(), emptyCollectionOf(String.class));
assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)));
EqualTo
import static org.hamcrest.Matchers.equalTo;
assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)));
HasSize
import static org.hamcrest.Matchers.hasSize;
assertThat(Arrays.asList("foo", "bar"), hasSize(2));
Multiple Matchers
AllOf
import static org.hamcrest.Matchers.allOf;
boolean a = true;
boolean b = true;
boolean c = false;
assertThat(a, allOf(is(b), not(c)));
assertThat(a, allOf(is(b)));
AnyOf
import static org.hamcrest.Matchers.anyOf;
boolean a = true;
boolean b = true;
boolean c = false;
assertThat(a, anyOf(is(c), not(c)));
assertThat(a, anyOf(is(b)));
DescribedAs
import static org.hamcrest.Matchers.describedAs;
describedAs(description, equalTo(bigDecimal), bigDecimal.toPlainString());
EqualTo
import static org.hamcrest.Matchers.equalTo;
logger.info(String.valueOf(describedAs(description, equalTo(bigDecimal), bigDecimal.toPlainString())));
Is
import static org.hamcrest.Matchers.is;
boolean a = true;
boolean b = false;
assertThat(a, is(not(b)));
Not
import static org.hamcrest.Matchers.not;
boolean a = true;
boolean b = false;
assertThat(a, not(is(b)));
Comparables
EqualTo
import static org.hamcrest.Matchers.comparesEqualTo;
assertThat(1, comparesEqualTo(1));
GreaterThan
import static org.hamcrest.Matchers.greaterThan;
assertThat(2, greaterThan(1));
GreaterThanOrEqualTo
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
assertThat(1, greaterThanOrEqualTo(1));
assertThat(2, greaterThanOrEqualTo(1));
LessThan
import static org.hamcrest.Matchers.lessThan;
assertThat(1, lessThan(2));
LessThanOrEqualTo
import static org.hamcrest.Matchers.lessThanOrEqualTo;
assertThat(1, lessThanOrEqualTo(1));
assertThat(1, lessThanOrEqualTo(2));
DOM
HasXPath
import static org.hamcrest.Matchers.hasXPath;
assertThat(testNode, hasXPath("/xml/top/middle/bottom"));
StartsWith
import static org.hamcrest.Matchers.startsWith;
assertThat(testNode, hasXPath("/xml/top/middle/bottom", startsWith("val")));
EventObjects
EventFrom
import static org.hamcrest.Matchers.eventFrom;
assertThat(testEvent, eventFrom(source));
assertThat(testEvent, is(eventFrom(source)));
Is
import static org.hamcrest.Matchers.is;
assertThat(testMenuEvent, is(eventFrom(MenuEvent.class, source)));
assertThat(testMenuEvent, eventFrom(MenuEvent.class, source));
General purpose
Any
import static org.hamcrest.Matchers.any;
assertThat(myString, any(String.class));
Anything
import static org.hamcrest.Matchers.anything;
// All the following assertions match always
assertThat(myString, is(anything()));
assertThat(four, is(anything()));
// Same thing, with just a description of this "anything"
assertThat(four, is(anything("four")));
EqualTo
import static org.hamcrest.Matchers.equalTo;
assertThat(a, equalTo(b));
HasToString
import static org.hamcrest.Matchers.hasToString;
assertThat(boolObject, hasToString("true"));
// Check that toString method of boolObject object returns a value that satisfies the specified matcher
assertThat(boolObject, hasToString(any(String.class)));
InstanceOf
import static org.hamcrest.Matchers.instanceOf;
assertThat(four, instanceOf(Integer.class));
assertThat(four, is(instanceOf(Integer.class)));
Is
import static org.hamcrest.Matchers.is;
assertThat(a, is(b));
// Deprecated
assertThat(a, is(Boolean.class));
IsA
import static org.hamcrest.Matchers.isA;
assertThat(four, isA(Integer.class));
IsIn
import static org.hamcrest.Matchers.isIn;
assertThat(four, isIn(numbers));
// Check that object is within the numbers array
assertThat(four, isIn(numberAsArray));
IsOneOf
import static org.hamcrest.Matchers.isOneOf;
assertThat(four, isOneOf(four, five));
Not
import static org.hamcrest.Matchers.not;
assertThat(a, not(c));
NotNullValue
import static org.hamcrest.Matchers.notNullValue;
assertThat(four, notNullValue());
assertThat(four, is(notNullValue()));
// Check that four is not a null variable of Integer type
assertThat(four, notNullValue(Integer.class));
NullValue
import static org.hamcrest.Matchers.nullValue;
// Check that nullValue variable is null
assertThat(nullValue, nullValue());
assertThat(nullValue, is(nullValue()));
// Check that nullValue is a null variable of String type
assertThat(nullValue, nullValue(String.class));
SameInstance
import static org.hamcrest.Matchers.sameInstance;
assertThat(four, sameInstance(four));
assertThat(four, is(sameInstance(four)));
assertThat(four, not(sameInstance(five)));
TheInstance
import static org.hamcrest.Matchers.theInstance;
assertThat(four, theInstance(four));
assertThat(four, is(theInstance(four)));
assertThat(four, not(theInstance(five)));
Iterables
Contains
import static org.hamcrest.Matchers.contains;
assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"));
// With one matcher as parameter
assertThat(Arrays.asList("foo"), contains(equalTo("foo")));
// With matchers as parameters
assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")));
// With list of matchers as parameter
List<org.hamcrest.Matcher<? super String>> matchers = new ArrayList<org.hamcrest.Matcher<? super String>>();
matchers.add(equalTo("foo"));
matchers.add(equalTo("bar"));
assertThat(Arrays.asList("foo", "bar"), contains(matchers));
ContainsInAnyOrder
import static org.hamcrest.Matchers.containsInAnyOrder;
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"));
// With one matcher as parameter
assertThat(Arrays.asList("foo"), containsInAnyOrder(equalTo("foo")));
// With matchers as parameters
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")));
// With list of matchers as parameter
List<org.hamcrest.Matcher<? super String>> matchers = new ArrayList<org.hamcrest.Matcher<? super String>>();
matchers.add(equalTo("bar"));
matchers.add(equalTo("foo"));
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(matchers));
EmptyIterable
import static org.hamcrest.Matchers.emptyIterable;
assertThat(new ArrayList<String>(), emptyIterable());
assertThat(new ArrayList<String>(), is(emptyIterable()));
EmptyIterableOf
import static org.hamcrest.Matchers.emptyIterableOf;
assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)));
EveryItem
import static org.hamcrest.Matchers.everyItem;
assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")));
HasItem
import static org.hamcrest.Matchers.hasItem;
assertThat(Arrays.asList("foo", "bar"), hasItem("bar"));
// With Matcher as paramter
assertThat(Arrays.asList("foo", "bar"), hasItem(equalTo("bar")));
HasItems
import static org.hamcrest.Matchers.hasItems;
assertThat(Arrays.asList("foo", "bar", "toto"), hasItems("bar", "toto"));
// With Matcher as paramter
assertThat(Arrays.asList("foo", "bar", "toto"), hasItems(equalTo("bar"), equalTo("toto")));
Maps
HasEntry
import static org.hamcrest.Matchers.hasEntry;
assertThat(myMap, hasEntry("bar", "foo"));
// With matchers as parameters
assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")));
HasKey
import static org.hamcrest.Matchers.hasKey;
assertThat(myMap, hasKey("bar"));
// With matcher as parameter
assertThat(myMap, hasKey(equalTo("bar")));
HasValue
import static org.hamcrest.Matchers.hasValue;
assertThat(myMap, hasValue("foo"));
// With matcher as parameter
assertThat(myMap, hasValue(equalTo("foo")));
Numbers
CloseTo
import static org.hamcrest.Matchers.closeTo;
// Creates a matcher of BigDecimals that matches when an examined BigDecimal is equal to
// the specified operand, within a range of +/- error. The comparison for equality is done
// by BigDecimals BigDecimal.compareTo(java.math.BigDecimal) method.
// The first parameter is the operand: the expected value of matching BigDecimals
// Second parameter is error: the delta (+/-) within which matches will be allowed
// => summary: 1.03 is close to 1.0 with - 0.03 error (precision)
assertThat(new BigDecimal("1.03"), closeTo(new BigDecimal("1.0"), new BigDecimal("0.03")));
assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))));
// With doubles
assertThat(1.5, closeTo(1.0, 0.5));
assertThat(1.5, is(closeTo(1.0, 0.5)));
Strings
ContainsString
import static org.hamcrest.Matchers.containsString;
assertThat(value, containsString("ring"));
EndsWith
import static org.hamcrest.Matchers.endsWith;
assertThat(value, endsWith("Note"));
EqualToIgnoringCase
import static org.hamcrest.Matchers.equalToIgnoringCase;
String value = "myStringOfNoTe";
assertThat(value, equalToIgnoringCase("MYSTRINGOFNOTE"));
EqualToIgnoringWhiteSpace
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
String value = "myStringOfNoTe";
assertThat(value, equalToIgnoringWhiteSpace(" myStringOfNoTe "));
IsEmptyOrNullString
import static org.hamcrest.Matchers.isEmptyOrNullString;
assertThat("", isEmptyOrNullString());
assertThat(null, isEmptyOrNullString());
IsEmptyString
import static org.hamcrest.Matchers.isEmptyString;
assertThat("", isEmptyString());
StartsWith
import static org.hamcrest.Matchers.startsWith;
assertThat(value, startsWith("my"));
StringContainsInOrder
import static org.hamcrest.Matchers.stringContainsInOrder;
assertThat("myString Of Note", stringContainsInOrder(Arrays.asList("my", "String", "Of", "Note")));
Inhaltsverzeichnis