Wednesday, 4 April 2018

Hamcrest: hasKey: Check for the existence of key

By using 'haskey method, you can create a matcher for Maps to match when the examined Map contains at least one key that is equal to the specified key.

Ex
Map<String, String> employeeMap = new HashMap<>();
                 
employeeMap.put("I1234", "Krishna Gurram");
employeeMap.put("I346", "Chamu M");
employeeMap.put("I546", "Sri Ram");
                 
assertThat("employeeMap must contain the key 'I346'",employeeMap, hasKey("I346"));

TestApp.java
package com.sample.tests;

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

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  Map<String, String> employeeMap = new HashMap<>();
  
  employeeMap.put("I1234", "Krishna Gurram");
  employeeMap.put("I346", "Chamu M");
  employeeMap.put("I546", "Sri Ram");
  
  assertThat("employeeMap must contain the key 'I346'",employeeMap, hasKey("I346"));
  
 }
}



Previous                                                 Next                                                 Home

Hamcrest: hasEntry: Check for the entry in map

public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K, ? extends V>> hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher)
By using ‘hasEntry’ method, you can create a matcher for Maps to match when the examined Map contains at least one entry whose key satisfies the specified keyMatcher and whose value satisfies the specified valueMatcher.

Ex
Map<String, String> employeeMap = new HashMap<>();
                 
employeeMap.put("I1234", "Krishna Gurram");
employeeMap.put("I346", "Chamu M");
employeeMap.put("I546", "Sri Ram");
                 
assertThat("employeeMap must contain <I346, Chamu M>",employeeMap, hasEntry(equalTo("I346"), equalTo("Chamu M")));

Find the below working application.

TestApp.java
package com.sample.tests;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.equalTo;

public class TestApp {

 @Test
 public void testmethod() {
  Map<String, String> employeeMap = new HashMap<>();
  
  employeeMap.put("I1234", "Krishna Gurram");
  employeeMap.put("I346", "Chamu M");
  employeeMap.put("I546", "Sri Ram");
  
  assertThat("employeeMap must contain <I346, Chamu M>",employeeMap, hasEntry(equalTo("I346"), equalTo("Chamu M")));
  
 }
}



Previous                                                 Next                                                 Home

Hamcrest: iterableWithSize: Check the size of iterable

By using 'iterableWithSize’ method, you can create a matcher that is used to check the size of iterable.

Ex
assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(3));
                           (or)
assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(equalTo(3)));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;

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

import org.hamcrest.collection.IsIterableWithSize;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  List<Integer> list = Arrays.asList(5, 3, 2);

  assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(3));
  
  // You can also compare like below.
  assertThat(list, IsIterableWithSize.<Integer> iterableWithSize(equalTo(3)));
 }
}



Previous                                                 Next                                                 Home

Hamcrest: containsInAnyOrder: Search the elements of collection (order doesn’t matter)

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));
 }
}






Previous                                                 Next                                                 Home

Hamcrest: contains: Check for the existence of elements in the collection

By using ‘contains’ method, you can check whether given elements exist in the collection or not.

‘contains’ method is available in overloaded form.

public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(E... items)
By using this method 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.

Ex
List<Integer> list = Arrays.asList(2, 3, 5, 7, 11);
assertThat("list must contain 2, 3, 5, 7, 11", list, contains(2, 3, 5, 7, 11));

Find the below working application.

TestApp.java
package com.sample.tests;

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

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, contains(2, 3, 5, 7, 11));
 }
}

public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E>... 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, contains(equalTo(2), equalTo(3), greaterThan(10)));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
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, contains(equalTo(2), equalTo(3), greaterThan(10)));
 }
}


public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(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, contains(equalTo(2)));


TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
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, contains(equalTo(2)));
 }
}

public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<? super E>> 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 list of matchers.

Ex
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, contains(itemMatchers));

Find the below working application.


TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
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(2, 3, 5);

  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, contains(itemMatchers));
 }
}




Previous                                                 Next                                                 Home

Hamcrest: emptyIterableOf: Check the emptiness of iterable

By using ‘emptyIterableOf’ method, you can create a matcher for Iterables matching examined iterables that yield no items.

Ex
List<Integer> tempList = Arrays.asList();
assertThat("empty must be empty", tempList, emptyIterableOf(Integer.class));

Find the below working application.

TestApp.java
package com.sample.tests;

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

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

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  List<Integer> tempList = Arrays.asList();
  assertThat("empty must be empty", tempList, emptyIterableOf(Integer.class));
 }
}



Previous                                                 Next                                                 Home

Hamcrest: emptyIterable: Check the emptiness of iterables

By using ‘emptyIterable’ method, you can create a matcher for Iterables matching examined iterables that yield no items.

Ex
List<Integer> tempList = Arrays.asList();
assertThat("empty must be empty", tempList, emptyIterable());

Find the below working application.

TestApp.java
package com.sample.tests;

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

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

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  List<Integer> tempList = Arrays.asList();
  assertThat("empty must be empty", tempList, emptyIterable());
 }
}




Previous                                                 Next                                                 Home