By
using containsInAnyOrder method, you can check whether given elements exist in
the collection or not (Order of the elements is not considered).
'containsInAnyOrder'
method is available in different overloaded forms.
public static
<T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>>
containsInAnyOrder(T... items)
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 logically
equal to the corresponding item in the specified items. Order of the elements
doesn't matter while checking.
Ex
List<Integer>
list = Arrays.asList(2, 3, 5, 7, 11);
assertThat("list
must contain 2, 3, 5, 7, 11", list, containsInAnyOrder(3, 7, 11, 5, 2));
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; 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, containsInAnyOrder(3, 7, 11, 5, 2)); } }
public static
<T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>>
containsInAnyOrder(org.hamcrest.Matcher<? super T>... 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, containsInAnyOrder(greaterThan(10),
equalTo(3), equalTo(2)));
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; 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, containsInAnyOrder(greaterThan(10), equalTo(3), equalTo(2))); } }
public static <E>
org.hamcrest.Matcher<java.lang.Iterable<? extends E>>
containsInAnyOrder(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, containsInAnyOrder(equalTo(2)));
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; 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, containsInAnyOrder(equalTo(2))); } }
public static
<T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>>
containsInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super
T>> 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 collection of matchers
(Order of the elements doesn't matter).
Ex
List<Integer>
list = Arrays.asList(5, 3, 2);
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, containsInAnyOrder(itemMatchers));
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; 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(5, 3, 2); 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, containsInAnyOrder(itemMatchers)); } }
No comments:
Post a Comment