Saturday 24 March 2018

Hamcrest: hasItems: Check for the existence of multiple items

By using ‘hasItems’ method, you can create a matcher that is used to check existence of multiple items in the collection.

‘hasItems’ method is available in two overloaded forms.

public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items)
Above method is used to check existence of multiple items in the collection.

Ex
a. Check the existence ot 45 and 24 in employeeAges
List<Integer> employeeAges = Arrays.asList(24, 45, 32);
assertThat("employee ages must have 45 and 24", employeeAges, hasItems(45, 24));

b. Check the existence of ‘melt’ and ‘men’ in strs.
List<String> strs = Arrays.asList("men", "met", "melt");
assertThat("strs must have 'melt' and 'men'", strs, hasItems("melt", "men"));

Find the below working application.
TestApp.java

package com.sample.tests;

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

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  List<Integer> employeeAges = Arrays.asList(24, 45, 32);
  assertThat("employee ages must have 45 and 24", employeeAges, hasItems(45, 24));

  List<String> strs = Arrays.asList("men", "met", "melt");
  assertThat("strs must have 'melt' and 'men'", strs, hasItems("melt", "men"));
 }
}

public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers)
By using this method, you can create a matcher for that matches at least one item that is matched by the corresponding matcher from the specified itemMatchers.

Ex
a. Make sure that the collection has an element that is equal to 32 and one element that is > 40.
List<Integer> employeeAges = Arrays.asList(24, 45, 32);
assertThat("Age must be > 20", employeeAges, hasItems(equalTo(32), greaterThan(40)));

b. Make sure that the collection has an element that is equal to ‘melt’ and element that ends with ‘et’.
List<String> strs = Arrays.asList("men", "met", "melt");
assertThat("elements shoudl start with me", strs, hasItems(equalTo("melt"), endsWith("et")));

Find the below working application.


TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItems;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class TestApp {

 @SuppressWarnings("unchecked")
 @Test
 public void testmethod() {

  List<Integer> employeeAges = Arrays.asList(24, 45, 32);
  assertThat("Age must be > 20", employeeAges, hasItems(equalTo(32), greaterThan(40)));

  List<String> strs = Arrays.asList("men", "met", "melt");
  assertThat("elements shoudl start with me", strs, hasItems(equalTo("melt"), endsWith("et")));
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment