Introduces Matchers that check the data of Collections such as List.
Set project dependencies
Gradle project adds java-hamcrest
as below in java-hamcrest
.
dependencies {
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
}
hasItem()
hasItem is a Matcher that checks which items the Collections have.
This can be used to check what values a list has, like this:
@Test
public void testCollectionHasItem() {
List<String> list = Arrays.asList("a", "b", "c");
assertThat(list, hasItem("a"));
assertThat(list, not(hasItem("d")));
}
hasItems()
You can use hasItems when checking multiple items together instead of one. The order in which they are passed as arguments to hasItems does not matter. If the passed-in items are all in the list, Matcher returns true.
@Test
public void testCollectionHasItems() {
List<String> list = Arrays.asList("a", "b", "c");
assertThat(list, hasItems("c", "a"));
assertThat(list, hasItems("a", "b", "c"));
}
containsInAnyOrder()
containsInAnyOrder, same as hasItems, is a Matcher used to check the number of items a list has.
@Test
public void testArrayContainsInAnyOrder() {
List<String> list = Arrays.asList("a", "b", "c");
assertThat(list, containsInAnyOrder("c", "b", "a"));
}
contains()
If you also need to compare the order of items stored in the list, you can use contains()
.
contains returns false when the order is different.
@Test
public void testCollectionContains() {
List<String> list = Arrays.asList("a", "b", "c");
assertThat(list, contains("a", "b", "c"));
assertThat(list, not(contains("c", "b", "a")));
}
empty()
You can use empty()
to check if Collections such as List, Set, etc. are empty.
List<String> list = Collections.emptyList();
assertThat(list, empty());
Set<String> set = new HashSet<>();
assertThat(set, empty());
In the case of Map, you can check for empty as follows.
Map<String, String> map = new HashMap();
assertThat(map, anEmptyMap());
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)