Thursday 8 March 2018

Hamcrest: arrayContaining: Match each item in the array

‘arrayContaining’ matcher is used to match each element in array. ‘arrayContaining’ matcher is available in 3 overloaded forms.

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

Ex
String names[] = { "krishna", "Chamu", "Sailu", "Gopi" };
assertThat("Checking names array", names, arrayContaining("krishna", "Chamu", "Sailu", "Gopi"));

Find the below working application.

TestApp.java
package com.sample.tests;

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

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String names[] = { "krishna", "Chamu", "Sailu", "Gopi" };
  assertThat("Checking names array", names, arrayContaining("krishna", "Chamu", "Sailu", "Gopi"));

 }
}

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

Ex
String names[] = { "krishna", "Chamu", "Sailu", "Gopi" };
assertThat("Checking names array", names, arrayContaining(startsWith("kri"), endsWith("mu"), notNullValue(), notNullValue()));

Find the below working application.


TestApp.java

package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;

import org.junit.Test;

public class TestApp {

 @SuppressWarnings("unchecked")
 @Test
 public void testmethod() {
  String names[] = { "krishna", "Chamu", "Sailu", "Gopi" };
  assertThat("Checking names array", names, arrayContaining(startsWith("kri"), endsWith("mu"), notNullValue(), notNullValue()));

 }
}

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

Ex
String names[] = { "krishna", "Chamu", "Sailu", "Gopi" };
List<org.hamcrest.Matcher<? super String>> itemMatchers = new ArrayList<Matcher<? super String>>();
itemMatchers.add(equalTo("krishna"));
itemMatchers.add(equalTo("Chamu"));
itemMatchers.add(endsWith("lu"));
itemMatchers.add(endsWith("pi"));

assertThat("Checking names array", names, arrayContaining(itemMatchers));

Find the below working application.


TestApp.java

package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
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 names[] = { "krishna", "Chamu", "Sailu", "Gopi" };
  List<org.hamcrest.Matcher<? super String>> itemMatchers = new ArrayList<Matcher<? super String>>();
  itemMatchers.add(equalTo("krishna"));
  itemMatchers.add(equalTo("Chamu"));
  itemMatchers.add(endsWith("lu"));
  itemMatchers.add(endsWith("pi"));

  assertThat("Checking names array", names, arrayContaining(itemMatchers));

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment