Best junit code snippet using org.hamcrest.core.IsEqual.matches
Source:CoreMatchers.java
1package org.hamcrest;2@SuppressWarnings("UnusedDeclaration")3public class CoreMatchers {4 /**5 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.6 * For example:7 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>8 */9 public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) {10 return org.hamcrest.core.AllOf.<T>allOf(matchers);11 }12 /**13 * Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.14 * For example:15 * <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>16 */17 @SafeVarargs18 public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T>... matchers) {19 return org.hamcrest.core.AllOf.<T>allOf(matchers);20 }21 /**22 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.23 * For example:24 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>25 */26 public static <T> org.hamcrest.core.AnyOf<T> anyOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) {27 return org.hamcrest.core.AnyOf.<T>anyOf(matchers);28 }29 /**30 * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.31 * For example:32 * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>33 */34 @SafeVarargs35 public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T>... matchers) {36 return org.hamcrest.core.AnyOf.<T>anyOf(matchers);37 }38 /**39 * Creates a matcher that matches when both of the specified matchers match the examined object.40 * For example:41 * <pre>assertThat("fab", both(containsString("a")).and(containsString("b")))</pre>42 */43 public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableBothMatcher<LHS> both(org.hamcrest.Matcher<? super LHS> matcher) {44 return org.hamcrest.core.CombinableMatcher.both(matcher);45 }46 /**47 * Creates a matcher that matches when either of the specified matchers match the examined object.48 * For example:49 * <pre>assertThat("fan", either(containsString("a")).or(containsString("b")))</pre>50 */51 public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher<LHS> either(org.hamcrest.Matcher<? super LHS> matcher) {52 return org.hamcrest.core.CombinableMatcher.either(matcher);53 }54 /**55 * Wraps an existing matcher, overriding its description with that specified. All other functions are56 * delegated to the decorated matcher, including its mismatch description.57 * For example:58 * <pre>describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())</pre>59 * 60 * @param description61 * the new description for the wrapped matcher62 * @param matcher63 * the matcher to wrap64 * @param values65 * optional values to insert into the tokenised description66 */67 public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) {68 return org.hamcrest.core.DescribedAs.describedAs(description, matcher, values);69 }70 /**71 * Creates a matcher for {@link Iterable}s that only matches when a single pass over the72 * examined {@link Iterable} yields items that are all matched by the specified73 * <code>itemMatcher</code>.74 * For example:75 * <pre>assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))</pre>76 * 77 * @param itemMatcher78 * the matcher to apply to every item provided by the examined {@link Iterable}79 */80 public static <U> org.hamcrest.Matcher<java.lang.Iterable<? extends U>> everyItem(org.hamcrest.Matcher<U> itemMatcher) {81 return org.hamcrest.core.Every.everyItem(itemMatcher);82 }83 /**84 * Decorates another Matcher, retaining its behaviour, but allowing tests85 * to be slightly more expressive.86 * For example:87 * <pre>assertThat(cheese, is(equalTo(smelly)))</pre>88 * instead of:89 * <pre>assertThat(cheese, equalTo(smelly))</pre>90 */91 public static <T> org.hamcrest.Matcher<T> is(org.hamcrest.Matcher<T> matcher) {92 return org.hamcrest.core.Is.is(matcher);93 }94 /**95 * A shortcut to the frequently used <code>is(equalTo(x))</code>.96 * For example:97 * <pre>assertThat(cheese, is(smelly))</pre>98 * instead of:99 * <pre>assertThat(cheese, is(equalTo(smelly)))</pre>100 */101 public static <T> org.hamcrest.Matcher<T> is(T value) {102 return org.hamcrest.core.Is.is(value);103 }104 /**105 * Provided to cause compile time error when used in preference to a possible runtime error if106 * this was not here.107 *108 * <p>This method was removed upstream between Hamcrest 1.1 and 1.3 in favour of the109 * instanceOf(Class) method. Unfortunately, existing usages of it could still compile against the110 * {@link #is(Object)} method instead. Although not every existing usage would compile111 * successfully it is possible that some could and that would result in a change in the runtime112 * behavior that could be difficult to detect and fix. This change aims to turn any significant113 * usage of this method into a compile time error.114 *115 * @deprecated Use instanceOf(SomeClass.class) instead.116 */117 public static void is(java.lang.Class<?> type) {118 }119 /**120 * A shortcut to the frequently used <code>is(instanceOf(SomeClass.class))</code>.121 * For example:122 * <pre>assertThat(cheese, isA(Cheddar.class))</pre>123 * instead of:124 * <pre>assertThat(cheese, is(instanceOf(Cheddar.class)))</pre>125 */126 public static <T> org.hamcrest.Matcher<T> isA(java.lang.Class<T> type) {127 return org.hamcrest.core.Is.isA(type);128 }129 /**130 * Creates a matcher that always matches, regardless of the examined object.131 */132 public static org.hamcrest.Matcher<java.lang.Object> anything() {133 return org.hamcrest.core.IsAnything.anything();134 }135 /**136 * Creates a matcher that always matches, regardless of the examined object, but describes137 * itself with the specified {@link String}.138 * 139 * @param description140 * a meaningful {@link String} used when describing itself141 */142 public static org.hamcrest.Matcher<java.lang.Object> anything(java.lang.String description) {143 return org.hamcrest.core.IsAnything.anything(description);144 }145 /**146 * Creates a matcher for {@link Iterable}s that only matches when a single pass over the147 * examined {@link Iterable} yields at least one item that is matched by the specified148 * <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable}149 * will stop as soon as a matching item is found.150 * For example:151 * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre>152 * 153 * @param itemMatcher154 * the matcher to apply to items provided by the examined {@link Iterable}155 */156 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) {157 return org.hamcrest.core.IsCollectionContaining.<T>hasItem(itemMatcher);158 }159 /**160 * Creates a matcher for {@link Iterable}s that only matches when a single pass over the161 * examined {@link Iterable} yields at least one item that is equal to the specified162 * <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable}163 * will stop as soon as a matching item is found.164 * For example:165 * <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre>166 * 167 * @param item168 * the item to compare against the items provided by the examined {@link Iterable}169 */170 public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) {171 return org.hamcrest.core.IsCollectionContaining.<T>hasItem(item);172 }173 /**174 * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the175 * examined {@link Iterable} yield at least one item that is matched by the corresponding176 * matcher from the specified <code>itemMatchers</code>. Whilst matching, each traversal of177 * the examined {@link Iterable} will stop as soon as a matching item is found.178 * For example:179 * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))</pre>180 * 181 * @param itemMatchers182 * the matchers to apply to items provided by the examined {@link Iterable}183 */184 @SafeVarargs185 public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) {186 return org.hamcrest.core.IsCollectionContaining.<T>hasItems(itemMatchers);187 }188 /**189 * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the190 * examined {@link Iterable} yield at least one item that is equal to the corresponding191 * item from the specified <code>items</code>. Whilst matching, each traversal of the192 * examined {@link Iterable} will stop as soon as a matching item is found.193 * For example:194 * <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre>195 * 196 * @param items197 * the items to compare against the items provided by the examined {@link Iterable}198 */199 @SafeVarargs200 public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) {201 return org.hamcrest.core.IsCollectionContaining.<T>hasItems(items);202 }203 /**204 * Creates a matcher that matches when the examined object is logically equal to the specified205 * <code>operand</code>, as determined by calling the {@link java.lang.Object#equals} method on206 * the <b>examined</b> object.207 * 208 * <p>If the specified operand is <code>null</code> then the created matcher will only match if209 * the examined object's <code>equals</code> method returns <code>true</code> when passed a210 * <code>null</code> (which would be a violation of the <code>equals</code> contract), unless the211 * examined object itself is <code>null</code>, in which case the matcher will return a positive212 * match.</p>213 * 214 * <p>The created matcher provides a special behaviour when examining <code>Array</code>s, whereby215 * it will match if both the operand and the examined object are arrays of the same length and216 * contain items that are equal to each other (according to the above rules) <b>in the same217 * indexes</b>.</p> 218 * For example:219 * <pre>220 * assertThat("foo", equalTo("foo"));221 * assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"}));222 * </pre>223 */224 public static <T> org.hamcrest.Matcher<T> equalTo(T operand) {225 return org.hamcrest.core.IsEqual.equalTo(operand);226 }227 /**228 * Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being229 * compared to be of the same static type.230 */231 public static org.hamcrest.Matcher<java.lang.Object> equalToObject(java.lang.Object operand) {232 return org.hamcrest.core.IsEqual.equalToObject(operand);233 }234 /**235 * Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>,236 * as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the237 * the examined object.238 * 239 * <p>The created matcher forces a relationship between specified type and the examined object, and should be240 * used when it is necessary to make generics conform, for example in the JMock clause241 * <code>with(any(Thing.class))</code></p>242 * For example:243 * <pre>assertThat(new Canoe(), instanceOf(Canoe.class));</pre>244 */245 public static <T> org.hamcrest.Matcher<T> any(java.lang.Class<T> type) {246 return org.hamcrest.core.IsInstanceOf.any(type);247 }248 /**249 * Creates a matcher that matches when the examined object is an instance of the specified <code>type</code>,250 * as determined by calling the {@link java.lang.Class#isInstance(Object)} method on that type, passing the251 * the examined object.252 * 253 * <p>The created matcher assumes no relationship between specified type and the examined object.</p>254 * For example:255 * <pre>assertThat(new Canoe(), instanceOf(Paddlable.class));</pre>256 */257 public static <T> org.hamcrest.Matcher<T> instanceOf(java.lang.Class<?> type) {258 return org.hamcrest.core.IsInstanceOf.instanceOf(type);259 }260 /**261 * Creates a matcher that wraps an existing matcher, but inverts the logic by which262 * it will match.263 * For example:264 * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>265 * 266 * @param matcher267 * the matcher whose sense should be inverted268 */269 public static <T> org.hamcrest.Matcher<T> not(org.hamcrest.Matcher<T> matcher) {270 return org.hamcrest.core.IsNot.not(matcher);271 }272 /**273 * A shortcut to the frequently used <code>not(equalTo(x))</code>.274 * For example:275 * <pre>assertThat(cheese, is(not(smelly)))</pre>276 * instead of:277 * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>278 * 279 * @param value280 * the value that any examined object should <b>not</b> equal281 */282 public static <T> org.hamcrest.Matcher<T> not(T value) {283 return org.hamcrest.core.IsNot.not(value);284 }285 /**286 * A shortcut to the frequently used <code>not(nullValue())</code>.287 * For example:288 * <pre>assertThat(cheese, is(notNullValue()))</pre>289 * instead of:290 * <pre>assertThat(cheese, is(not(nullValue())))</pre>291 */292 public static org.hamcrest.Matcher<java.lang.Object> notNullValue() {293 return org.hamcrest.core.IsNull.notNullValue();294 }295 /**296 * A shortcut to the frequently used <code>not(nullValue(X.class)). Accepts a297 * single dummy argument to facilitate type inference.</code>.298 * For example:299 * <pre>assertThat(cheese, is(notNullValue(X.class)))</pre>300 * instead of:301 * <pre>assertThat(cheese, is(not(nullValue(X.class))))</pre>302 * 303 * @param type304 * dummy parameter used to infer the generic type of the returned matcher305 */306 public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type) {307 return org.hamcrest.core.IsNull.notNullValue(type);308 }309 /**310 * Creates a matcher that matches if examined object is <code>null</code>.311 * For example:312 * <pre>assertThat(cheese, is(nullValue())</pre>313 */314 public static org.hamcrest.Matcher<java.lang.Object> nullValue() {315 return org.hamcrest.core.IsNull.nullValue();316 }317 /**318 * Creates a matcher that matches if examined object is <code>null</code>. Accepts a319 * single dummy argument to facilitate type inference.320 * For example:321 * <pre>assertThat(cheese, is(nullValue(Cheese.class))</pre>322 * 323 * @param type324 * dummy parameter used to infer the generic type of the returned matcher325 */326 public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type) {327 return org.hamcrest.core.IsNull.nullValue(type);328 }329 /**330 * Creates a matcher that matches only when the examined object is the same instance as331 * the specified target object.332 * 333 * @param target334 * the target instance against which others should be assessed335 */336 public static <T> org.hamcrest.Matcher<T> sameInstance(T target) {337 return org.hamcrest.core.IsSame.sameInstance(target);338 }339 /**340 * Creates a matcher that matches only when the examined object is the same instance as341 * the specified target object.342 * 343 * @param target344 * the target instance against which others should be assessed345 */346 public static <T> org.hamcrest.Matcher<T> theInstance(T target) {347 return org.hamcrest.core.IsSame.theInstance(target);348 }349 /**350 * Creates a matcher that matches if the examined {@link String} contains the specified351 * {@link String} anywhere.352 * For example:353 * <pre>assertThat("myStringOfNote", containsString("ring"))</pre>354 * 355 * @param substring356 * the substring that the returned matcher will expect to find within any examined string357 */358 public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.String substring) {359 return org.hamcrest.core.StringContains.containsString(substring);360 }361 /**362 * Creates a matcher that matches if the examined {@link String} contains the specified363 * {@link String} anywhere, ignoring case.364 * For example:365 * <pre>assertThat("myStringOfNote", containsString("ring"))</pre>366 * 367 * @param substring368 * the substring that the returned matcher will expect to find within any examined string369 */370 public static org.hamcrest.Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring) {371 return org.hamcrest.core.StringContains.containsStringIgnoringCase(substring);372 }373 /**374 * <p>375 * Creates a matcher that matches if the examined {@link String} starts with the specified376 * {@link String}.377 * </p>378 * For example:379 * <pre>assertThat("myStringOfNote", startsWith("my"))</pre>380 * 381 * @param prefix382 * the substring that the returned matcher will expect at the start of any examined string383 */384 public static org.hamcrest.Matcher<java.lang.String> startsWith(java.lang.String prefix) {385 return org.hamcrest.core.StringStartsWith.startsWith(prefix);386 }387 /**388 * <p>389 * Creates a matcher that matches if the examined {@link String} starts with the specified390 * {@link String}, ignoring case391 * </p>392 * For example:393 * <pre>assertThat("myStringOfNote", startsWith("my"))</pre>394 * 395 * @param prefix396 * the substring that the returned matcher will expect at the start of any examined string397 */398 public static org.hamcrest.Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix) {399 return org.hamcrest.core.StringStartsWith.startsWithIgnoringCase(prefix);400 }401 /**402 * Creates a matcher that matches if the examined {@link String} ends with the specified403 * {@link String}.404 * For example:405 * <pre>assertThat("myStringOfNote", endsWith("Note"))</pre>406 * 407 * @param suffix408 * the substring that the returned matcher will expect at the end of any examined string409 */410 public static org.hamcrest.Matcher<java.lang.String> endsWith(java.lang.String suffix) {411 return org.hamcrest.core.StringEndsWith.endsWith(suffix);412 }413 /**414 * Creates a matcher that matches if the examined {@link String} ends with the specified415 * {@link String}, ignoring case.416 * For example:417 * <pre>assertThat("myStringOfNote", endsWith("Note"))</pre>418 * 419 * @param suffix420 * the substring that the returned matcher will expect at the end of any examined string421 */422 public static org.hamcrest.Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix) {423 return org.hamcrest.core.StringEndsWith.endsWithIgnoringCase(suffix);424 }425}...
Source:DefaultSetOperationsTests.java
...76 }77 @SuppressWarnings("unchecked")78 @Test79 public void testDistinctRandomMembers() {80 assumeTrue(RedisTestProfileValueSource.matches("redisVersion", "2.6"));81 K setKey = keyFactory.instance();82 V v1 = valueFactory.instance();83 V v2 = valueFactory.instance();84 V v3 = valueFactory.instance();85 setOps.add(setKey, v1);86 setOps.add(setKey, v2);87 setOps.add(setKey, v3);88 Set<V> members = setOps.distinctRandomMembers(setKey, 2);89 assertEquals(2, members.size());90 Set<V> expected = new HashSet<V>();91 expected.add(v1);92 expected.add(v2);93 expected.add(v3);94 assertThat(expected, hasItems((V[]) members.toArray()));95 }96 @SuppressWarnings("unchecked")97 @Test98 public void testRandomMembersWithDuplicates() {99 assumeTrue(RedisTestProfileValueSource.matches("redisVersion", "2.6"));100 K setKey = keyFactory.instance();101 V v1 = valueFactory.instance();102 V v2 = valueFactory.instance();103 setOps.add(setKey, v1);104 setOps.add(setKey, v2);105 List<V> members = setOps.randomMembers(setKey, 2);106 assertEquals(2, members.size());107 108 assertThat(members, CoreMatchers.<Iterable<? super V>>either(hasItem(v1)).or(hasItem(v2)));109 }110 @Test111 public void testRandomMembersNegative() {112 assumeTrue(RedisTestProfileValueSource.matches("redisVersion", "2.6"));113 try {114 setOps.randomMembers(keyFactory.instance(), -1);115 fail("IllegalArgumentException should be thrown");116 } catch (IllegalArgumentException e) {}117 }118 @Test119 public void testDistinctRandomMembersNegative() {120 assumeTrue(RedisTestProfileValueSource.matches("redisVersion", "2.6"));121 try {122 setOps.distinctRandomMembers(keyFactory.instance(), -2);123 fail("IllegalArgumentException should be thrown");124 } catch (IllegalArgumentException e) {}125 }126 @SuppressWarnings("unchecked")127 @Test128 public void testMove() {129 K key1 = keyFactory.instance();130 K key2 = keyFactory.instance();131 V v1 = valueFactory.instance();132 V v2 = valueFactory.instance();133 setOps.add(key1, v1);134 setOps.add(key1, v2);...
Source:IsCollectionContainingTest.java
...32 final Matcher<Iterable<? super String>> matcher2 = hasItem(equalTo("a"));33 assertMismatchDescription("", matcher2, new ArrayList<String>());34 }35 public void testDoesNotMatchNull() {36 assertDoesNotMatch("should not matches null", hasItem(equalTo("a")), null);37 }38 public void testHasAReadableDescription() {39 assertDescription("a collection containing \"a\"", hasItem(equalTo("a")));40 }41 42 public void testCanMatchItemWhenCollectionHoldsSuperclass() // Issue 2443 {44 final Set<Number> s = new HashSet<Number>();45 s.add(Integer.valueOf(2));46 assertThat(s, new IsCollectionContaining<Number>(new IsEqual<Number>(Integer.valueOf(2))));47 assertThat(s, IsCollectionContaining.hasItem(Integer.valueOf(2)));48 }49 @SuppressWarnings("unchecked")50 public void testMatchesAllItemsInCollection() {51 final Matcher<Iterable<String>> matcher1 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));52 assertMatches("should match list containing all items",53 matcher1,54 asList("a", "b", "c"));55 56 final Matcher<Iterable<String>> matcher2 = hasItems("a", "b", "c");57 assertMatches("should match list containing all items (without matchers)",58 matcher2,59 asList("a", "b", "c"));60 61 final Matcher<Iterable<String>> matcher3 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));62 assertMatches("should match list containing all items in any order",63 matcher3,64 asList("c", "b", "a"));65 66 final Matcher<Iterable<String>> matcher4 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));67 assertMatches("should match list containing all items plus others",68 matcher4,69 asList("e", "c", "b", "a", "d"));70 71 final Matcher<Iterable<String>> matcher5 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));72 assertDoesNotMatch("should not match list unless it contains all items",73 matcher5,74 asList("e", "c", "b", "d")); // 'a' missing75 }76 77 78 private static Matcher<? super String> mismatchable(final String string) {79 return new TypeSafeDiagnosingMatcher<String>() {80 @Override81 protected boolean matchesSafely(String item, Description mismatchDescription) {82 if (string.equals(item)) 83 return true;84 85 mismatchDescription.appendText("mismatched: " + item);86 return false;87 }88 @Override89 public void describeTo(Description description) {90 description.appendText("mismatchable: " + string);91 }92 };93 }94}...
Source:Behaviour.java
...11import org.mockito.Mockito;12import org.mockito.internal.progress.OngoingStubbing;13public class Behaviour {14 15 protected <T> void ensureThat(T obj, Matcher<T> matches) {16 MatcherAssert.assertThat(obj, matches);17 }18 protected <T> IsNull<T> isNull() {19 return new IsNull<T>();20 }21 protected <T> Matcher<T> eq(T object) {22 return new IsEqual<T>(object);23 }24 25 protected <T extends Comparable<T>> IsLessThan<T> isLessThan(T object) {26 return new IsLessThan<T>(object);27 }28 29 protected <T extends Comparable<T>> IsGreaterThanOrEq<T> isGreaterThanOrEq(T object) {30 return new IsGreaterThanOrEq<T>(object);31 }32 33 protected <T> T mock(Class<T> classToMock) {34 return Mockito.mock(classToMock);35 }36 protected <T> T verify(T mock) {37 return Mockito.verify(mock);38 }39 protected <T> IsInstanceOf isA(Class<T> clazz) {40 return new IsInstanceOf(clazz);41 }42 43 44 protected <T> OngoingStubbing<T> stub(T methodCall) {45 return Mockito.stub(methodCall);46 }47 protected void ensureThat(boolean expression) {48 Assert.assertTrue(expression);49 }50 protected Matcher<String> contains(String string) {51 return new StringContains(string);52 }53 public class IsGreaterThanOrEq<T extends Comparable<T>> extends TypeSafeMatcher<T> {54 private final Comparable<T> compareTo;55 56 public IsGreaterThanOrEq(Comparable<T> compareTo) {57 this.compareTo = compareTo;58 }59 60 public boolean matchesSafely(T item) {61 return compareTo.compareTo(item) <= 0;62 }63 64 public void describeTo(Description description) {65 description.appendText("a value greater than ");66 description.appendValue(compareTo);67 }68 }69 70 public class IsLessThan<T extends Comparable<T>> extends TypeSafeMatcher<T> {71 private final Comparable<T> compareTo;72 73 public IsLessThan(Comparable<T> compareTo) {74 this.compareTo = compareTo;75 }76 77 public boolean matchesSafely(T item) {78 return compareTo.compareTo(item) > 0;79 }80 81 public void describeTo(Description description) {82 description.appendText("a value greater than ");83 description.appendValue(compareTo);84 }85 }86}...
Source:IsMapContainingSingleton.java
...20 this.valueMatcher = valueMatcher;21 }22 23 @Override24 public boolean matchesSafely(Map<?, ?> map) {25 if(map != null && map.size() == 1){26 Entry<?, ?> singleEntry = map.entrySet().iterator().next();27 return keyMatcher.matches(singleEntry.getKey()) && 28 valueMatcher.matches(singleEntry.getValue());29 }30 return false;31 }32 @Override33 public void describeTo(Description description) {34 description.appendText("map containing unique entry [");35 keyMatcher.describeTo(description);36 description.appendText("->");37 valueMatcher.describeTo(description);38 description.appendText("]");39 }40 41 @Factory42 public static Matcher<Map<?, ?>> hasSingleEntry(Matcher<?> keyMatcher, Matcher<?> valueMatcher){...
Source:AnyOfTest.java
1package org.hamcrest.core;2import org.hamcrest.Matcher;3import org.junit.Test;4import static org.hamcrest.AbstractMatcherTest.*;5import static org.hamcrest.core.AnyOf.anyOf;6import static org.hamcrest.core.IsEqual.equalTo;7import static org.hamcrest.core.StringEndsWith.endsWith;8import static org.hamcrest.core.StringStartsWith.startsWith;9public final class AnyOfTest {10 @Test public void11 copesWithNullsAndUnknownTypes() {12 Matcher<String> matcher = anyOf(equalTo("irrelevant"), startsWith("irr"));13 14 assertNullSafe(matcher);15 assertUnknownTypeSafe(matcher);16 }17 @Test public void18 evaluatesToTheTheLogicalDisjunctionOfTwoOtherMatchers() {19 Matcher<String> matcher = anyOf(startsWith("goo"), endsWith("ood"));20 21 assertMatches("didn't pass both sub-matchers", matcher, "good");22 assertMatches("didn't pass second sub-matcher", matcher, "mood");23 assertMatches("didn't pass first sub-matcher", matcher, "goon");24 assertDoesNotMatch("didn't fail both sub-matchers", matcher, "flan");25 }26 @Test public void27 evaluatesToTheTheLogicalDisjunctionOfManyOtherMatchers() {28 Matcher<String> matcher = anyOf(startsWith("g"), startsWith("go"), endsWith("d"), startsWith("go"), startsWith("goo"));29 30 assertMatches("didn't pass middle sub-matcher", matcher, "vlad");31 assertDoesNotMatch("didn't fail all sub-matchers", matcher, "flan");32 }33 @SuppressWarnings("unchecked")34 @Test public void35 supportsMixedTypes() {36 final Matcher<SampleSubClass> matcher = anyOf(37 equalTo(new SampleBaseClass("bad")),38 equalTo(new SampleBaseClass("good")),39 equalTo(new SampleSubClass("ugly")));40 41 assertMatches("didn't pass middle sub-matcher", matcher, new SampleSubClass("good"));42 }43 @Test public void44 hasAReadableDescription() {45 assertDescription("(\"good\" or \"bad\" or \"ugly\")",46 anyOf(equalTo("good"), equalTo("bad"), equalTo("ugly")));47 }48}...
Source:IsTest.java
...13 assertNullSafe(matcher);14 assertUnknownTypeSafe(matcher);15 }16 @Test public void17 matchesTheSameWayTheUnderlyingMatcherDoes() {18 final Matcher<Boolean> matcher = is(equalTo(true));19 assertMatches(matcher, true);20 assertDoesNotMatch(matcher, false);21 }22 @Test public void23 generatesIsPrefixInDescription() {24 assertDescription("is <true>", is(equalTo(true)));25 assertDescription("is \"A\"", is("A"));26 }27 @Test public void28 providesConvenientShortcutForIsEqualTo() {29 final Matcher<String> matcher = is("A");30 31 assertMatches(matcher, "A");...
Source:Ensure.java
...5import org.hamcrest.core.IsNot;6import org.hamcrest.core.IsNull;7import org.hamcrest.core.IsSame;8public abstract class Ensure {9 public static void ensureThat(boolean matches) {10 ensureThat(matches, new IsSame<Boolean>(true));11 }12 public static <T> void ensureThat(T actual, Matcher<T> matcher) {13 assertThat(actual, matcher);14 }15 public static boolean not(boolean matches) {16 return !matches;17 }18 public static <T> Matcher<T> shouldBe(T expected) {19 return new IsEqual<T>(expected);20 }21 public static <T> Matcher<T> isNotNull() {22 return new IsNot<T>(new IsNull<T>());23 }24 public static <T> Matcher<T> isNull() {25 return new IsNull<T>();26 }27}...
matches
Using AI Code Generation
1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.core.IsEqual.equalTo;3public class IsEqualExample {4 public static void main(String[] args) {5 assertThat("Hello", equalTo("Hello"));6 assertThat("Hello", equalTo("Hello World"));7 }8}9 at org.hamcrest.core.IsEqualTest.testIsEqual(IsEqualTest.java:19)10 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)11 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)12 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)13 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)14 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)15 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)16 at org.hamcrest.core.IsEqualTest.testIsEqualWithLambda(IsEqualTest.java:28)
matches
Using AI Code Generation
1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.core.IsEqual.equalTo;3public class IsEqualExample {4 public static void main(String[] args) {5 assertThat("Hello World", equalTo("Hello World"));6 }7}8org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)9org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)10org.hamcrest.core.IsEqualExample.main(IsEqualExample.java:10)11import static org.hamcrest.MatcherAssert.assertThat;12import static org.hamcrest.core.IsEqual.equalTo;13public class IsEqualExample {14 public static void main(String[] args) {15 assertThat("Hello World", equalTo("Hello World"));16 }17}18org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)19org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)20org.hamcrest.core.IsEqualExample.main(IsEqualExample.java:10)21import static org.hamcrest.MatcherAssert.assertThat;22import static org.hamcrest.core.IsEqual.equalTo;23public class IsEqualExample {24 public static void main(String[] args) {25 assertThat("Hello World", equalTo("Hello World"));26 }27}28org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)29org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)30org.hamcrest.core.IsEqualExample.main(IsEqualExample.java:10)31import static org.hamcrest.MatcherAssert.assertThat;32import static org.hamcrest.core.IsEqual.equalToIgnoringCase;33public class IsEqualExample {34 public static void main(String[] args) {35 assertThat("Hello World", equalToIgnoringCase("hello world"));36 }37}38org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)39org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)40org.hamcrest.core.IsEqualExample.main(IsEqualExample.java:10)41import static org.hamcrest.MatcherAssert.assertThat;42import static org.hamcrest.core.IsEqual.equalToIgnoringWhiteSpace;43public class IsEqualExample {44 public static void main(String[] args) {45 assertThat("Hello World", equalToIgnoringWhiteSpace("Hello World"));46 assertThat("Hello World",
matches
Using AI Code Generation
1import static org.hamcrest.MatcherAssert.assertThat2import static org.hamcrest.core.IsEqual.equalTo3assertThat("Hello World", equalTo("Hello World"))4import static org.hamcrest.MatcherAssert.assertThat5import static org.hamcrest.core.IsNot.not6assertThat("Hello World", not(equalTo("Hello World")))7import static org.hamcrest.MatcherAssert.assertThat8import static org.hamcrest.core.Is.is9assertThat("Hello World", is("Hello World"))10import static org.hamcrest.MatcherAssert.assertThat11import static org.hamcrest.core.IsNull.nullValue12assertThat(null, nullValue())13import static org.hamcrest.MatcherAssert.assertThat14import static org.hamcrest.core.IsNull.notNullValue15assertThat("Hello World", notNullValue())16import static org.hamcrest.MatcherAssert.assertThat17import static org.hamcrest.core.IsNull.nullValue18assertThat(null, nullValue())19import static org.hamcrest.MatcherAssert.assertThat20import static org.hamcrest.core.IsNull.notNullValue21assertThat("Hello World", notNullValue())22import static org.hamcrest.MatcherAssert.assertThat23import static org.hamcrest.core.IsSame.sameInstance24assertThat("Hello World", sameInstance("Hello World"))25import static org.hamcrest.MatcherAssert.assertThat26import static org.hamcrest.core.IsSame.sameInstance27assertThat("Hello World", sameInstance("Hello World"))28import static org.hamcrest.MatcherAssert.assertThat29import static org.hamcrest.core.IsNot.not30assertThat("Hello World", not(sameInstance("Hello World")))31import static org.hamcrest.MatcherAssert.assertThat32import static org.hamcrest.core.IsNot.not33assertThat("Hello World", not(sameInstance("Hello World")))34import static org.hamcrest.MatcherAssert.assertThat35import static org.hamcrest.core.IsNot.not36assertThat("Hello World", not(sameInstance("Hello World")))
matches
Using AI Code Generation
1import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat;2@Test public void testAssertThatBothContainsString() { String str1 = "Junit is working fine"; String str2 = "Junit is working fine"; assertThat(str1, both(containsString("working")).and(containsString("fine"))); assertThat(str2, both(containsString("working")).and(containsString("fine"))); }3import static org.hamcrest.CoreMatchers.*; 4import static org.hamcrest.MatcherAssert.assertThat; 5public void testAssertThatBothContainsString() { 6 String str1 = "Junit is working fine"; 7 String str2 = "Junit is working fine"; 8 assertThat(str1, both(containsString("working")).and(containsString("fine"))); 9 assertThat(str2, both(containsString("working")).and(containsString("fine"))); 10}
matches
Using AI Code Generation
1import org.hamcrest.core.IsEqual2import org.junit.Assert.assertThat3import org.junit.Test4import org.junit.runner.RunWith5import org.junit.runners.Parameterized6import org.junit.runners.Parameterized.Parameters7@RunWith(Parameterized.class)8class IsEqualTest {9 static def data() {10 }11 public IsEqualTest(String input, String expected) {12 }13 public void testIsEqual() {14 assertThat(input, IsEqual.equalTo(expected))15 }16}17at org.junit.Assert.fail(Assert.java:88)18at org.junit.Assert.failNotEquals(Assert.java:743)19at org.junit.Assert.assertThat(Assert.java:518)20at org.junit.Assert.assertThat(Assert.java:497)21at IsEqualTest.testIsEqual(IsEqualTest.groovy:37)22at org.junit.Assert.assertEquals(Assert.java:115)23at org.junit.Assert.assertEquals(Assert.java:144)24at IsEqualTest.testIsEqual(IsEqualTest.groovy:37)25import org.hamcrest.core.IsEqual26import org.junit.Assert.assertThat27import org.junit.Test28import org.junit.runner.RunWith29import org
matches
Using AI Code Generation
1assertThat("Hello World", matches("Hello World"));2assertThat("Hello World", not(matches("Hello World")));3assertThat("Hello World", matches(Pattern.compile("Hello World")));4assertThat("Hello World", not(matches(Pattern.compile("Hello World"))));5assertThat("Hello World", matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE)));6assertThat("Hello World", not(matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE))));7assertThat("Hello World", matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)));8assertThat("Hello World", not(matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE))));9assertThat("Hello World", matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)));10assertThat("Hello World", not(matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL))));11assertThat("Hello World", matches(Pattern.compile("Hello World", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNICODE_CASE)));12assertThat("Hello World", not(matches
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!