Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 org.hamcrest.Matcher 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 org.hamcrest.Matcher 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 org.hamcrest.Matcher arrayContaining(java.util.List> 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> itemMatchers = new ArrayList>();
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" };
Listorg.hamcrest.Matcher super String>> itemMatchers = new ArrayListMatcher 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


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Hamcrest: arrayContaining: Match each item in the array

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×