Friday 9 March 2018

Hamcrest: arrayContainingInAnyOrder: Check array euality, order doesn't matter

By using 'arrayContainingInAnyOrder' you can create a matcher that checks the elements of the array, order doesn't matter.

'arrayContainingInAnyOrder' is available in 3 overloaded forms.

public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(E... items)
Creates a matcher for arrays that matches when each item in the examined array is logically equal to one item anywhere in the specified items.

Ex
String str[] = { "Hello", "How", "are", "you" };
assertThat("Checking array", str, arrayContainingInAnyOrder("you", "Hello", "How", "are"));

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String str1[] = { "Hello", "How", "are", "you" };
  assertThat("Checking array", str1, arrayContainingInAnyOrder("you", "Hello", "How", "are"));
  
  String str2[] = {"Hello", "Hello", "How"};
  assertThat("Checking array", str2, arrayContainingInAnyOrder("How", "Hello", "Hello"));
 }
}

public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>... itemMatchers)
It Create a matcher for arrays that matches when each item in the examined array satisfies one matcher anywhere in the specified matchers.

Ex
String str1[] = { "Hello", "How", "are", "you" };
assertThat("Checking array", str1, arrayContainingInAnyOrder(equalTo("you"), endsWith("o"), equalTo("How"), startsWith("are")));


TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;

import org.junit.Test;

public class TestApp {

 @SuppressWarnings("unchecked")
 @Test
 public void testmethod() {
  String str1[] = { "Hello", "How", "are", "you" };
  assertThat("Checking array", str1, arrayContainingInAnyOrder(equalTo("you"), endsWith("o"), equalTo("How"), startsWith("are")));
  
 }
}

public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers)
It creates a matcher for arrays that matches when each item in the examined array satisfies one matcher anywhere in the specified collection of matchers.      

Ex
String str1[] = { "Hello", "How", "are", "you" };

List<org.hamcrest.Matcher<? super String>> itemMatchers = new ArrayList<Matcher<? super String>>();
itemMatchers.add(equalTo("How"));
itemMatchers.add(equalTo("you"));
itemMatchers.add(endsWith("e"));
itemMatchers.add(endsWith("o"));

assertThat("Checking array", str1, arrayContainingInAnyOrder(itemMatchers));


TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String str1[] = { "Hello", "How", "are", "you" };

  List<org.hamcrest.Matcher<? super String>> itemMatchers = new ArrayList<Matcher<? super String>>();
  itemMatchers.add(equalTo("How"));
  itemMatchers.add(equalTo("you"));
  itemMatchers.add(endsWith("e"));
  itemMatchers.add(endsWith("o"));

  assertThat("Checking array", str1, arrayContainingInAnyOrder(itemMatchers));

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment