By
using ‘contains’ method, you can check whether given elements exist in the
collection or not.
‘contains’
method is available in overloaded form.
public static
<E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>>
contains(E... items)
By
using this method method, you can create a matcher for Iterables that matches
when a single pass over the examined Iterable yields a series of items, each
logically equal to the corresponding item in the specified items.
Ex
List<Integer>
list = Arrays.asList(2, 3, 5, 7, 11);
assertThat("list
must contain 2, 3, 5, 7, 11", list, contains(2, 3, 5, 7, 11));
Find
the below working application.
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import java.util.Arrays; import java.util.List; import org.junit.Test; public class TestApp { @Test public void testmethod() { List<Integer> list = Arrays.asList(2, 3, 5, 7, 11); assertThat("list must contain 2, 3, 5, 7, 11", list, contains(2, 3, 5, 7, 11)); } }
public static
<E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>>
contains(org.hamcrest.Matcher<? super E>... itemMatchers)
You
can create a matcher for Iterables that matches when a single pass over the
examined Iterable yields a series of items, each satisfying the corresponding
matcher in the specified matchers.
Ex
List<Integer>
list = Arrays.asList(2, 3, 11);
assertThat("list
must contain 2, 3, 5, 7, 11", list, contains(equalTo(2), equalTo(3),
greaterThan(10)));
Find
the below working application.
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import java.util.Arrays; import java.util.List; import org.junit.Test; public class TestApp { @SuppressWarnings("unchecked") @Test public void testmethod() { List<Integer> list = Arrays.asList(2, 3, 11); assertThat("list must contain 2, 3, 5, 7, 11", list, contains(equalTo(2), equalTo(3), greaterThan(10))); } }
public static
<E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>>
contains(org.hamcrest.Matcher<? super E> itemMatcher)
By
using this method, you can create a matcher for Iterables that matches when a
single pass over the examined Iterable yields a single item that satisfies the
specified matcher.
Ex
List<Integer>
list = Arrays.asList(2);
assertThat("list
must contain 2", list, contains(equalTo(2)));
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import java.util.Arrays; import java.util.List; import org.junit.Test; public class TestApp { @Test public void testmethod() { List<Integer> list = Arrays.asList(2); assertThat("list must contain 2", list, contains(equalTo(2))); } }
public static
<E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<?
super E>> itemMatchers) {
By
using this method, you can create a matcher for Iterables that matches when a
single pass over the examined Iterable yields a series of items, each
satisfying the corresponding matcher in the specified list of matchers.
Ex
List<org.hamcrest.Matcher<?
super Integer>> itemMatchers = new ArrayList<Matcher<? super
Integer>>();
itemMatchers.add(equalTo(2));
itemMatchers.add(equalTo(3));
itemMatchers.add(greaterThan(4));
assertThat("list
must contain 2, 3, and 5", list, contains(itemMatchers));
Find
the below working application.
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.hamcrest.Matcher; import org.junit.Test; public class TestApp { @Test public void testmethod() { List<Integer> list = Arrays.asList(2, 3, 5); List<org.hamcrest.Matcher<? super Integer>> itemMatchers = new ArrayList<Matcher<? super Integer>>(); itemMatchers.add(equalTo(2)); itemMatchers.add(equalTo(3)); itemMatchers.add(greaterThan(4)); assertThat("list must contain 2, 3, and 5", list, contains(itemMatchers)); } }
No comments:
Post a Comment