By
using ‘everyItem’ method, we can create a matcher that checks whether all the
elements are matched to given matcher or not.
Ex
a.
Employee age must be > 20
List<Integer>
employeeAges = Arrays.asList(24, 45, 32);
assertThat("Age
must be > 20", employeeAges, everyItem(greaterThan(20)));
b.
All the strings must start with word ‘me’.
List<String>
strs = Arrays.asList("men", "met", "melt");
assertThat("elements
shoudl start with me", strs, everyItem(startsWith("me")));
Find
the below working application.
TestApp.java
package com.sample.tests; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.startsWith; 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("Age must be > 20", employeeAges, everyItem(greaterThan(20))); List<String> strs = Arrays.asList("men", "met", "melt"); assertThat("elements shoudl start with me", strs, everyItem(startsWith("me"))); } }
No comments:
Post a Comment