Wednesday, 4 April 2018

Hamcrest: hasValue: Check the existence of value in map

By using hasValue method, you can create a matcher that is used to check whether map contains at least one value that is equal to the specified value.

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, hasValue("Sri Ram"));

Find the below working application.

TestApp.java
package com.sample.tests;

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

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 value Sri Ram", employeeMap, hasValue("Sri Ram"));

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment