Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2371ce

Browse files
author
Eugen Paraschiv
committed
testing examples for hamcrest
1 parent 31de9bb commit e2371ce

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.guava.collections;
1+
package org.baeldung.guava;
22

33
import static org.junit.Assert.assertNull;
44
import static org.junit.Assert.assertTrue;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.guava.collections;
1+
package org.baeldung.guava;
22

33
import static org.hamcrest.Matchers.equalTo;
44
import static org.hamcrest.Matchers.nullValue;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.baeldung.hamcrest;
2+
3+
import static org.hamcrest.Matchers.contains;
4+
import static org.hamcrest.Matchers.empty;
5+
import static org.hamcrest.Matchers.emptyArray;
6+
import static org.hamcrest.Matchers.equalTo;
7+
import static org.hamcrest.Matchers.hasItem;
8+
import static org.hamcrest.Matchers.hasItems;
9+
import static org.hamcrest.Matchers.not;
10+
import static org.junit.Assert.assertThat;
11+
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import org.junit.Test;
17+
18+
import com.google.common.collect.Lists;
19+
import com.google.common.collect.Maps;
20+
21+
public class HamcrestExamplesTest {
22+
23+
// tests
24+
25+
@Test
26+
public final void whenVerifyingSingleElementIsPartOfCollection_thenCorrect() {
27+
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
28+
assertThat(collection, hasItem("cd"));
29+
assertThat(collection, not(hasItem("zz")));
30+
}
31+
32+
@Test
33+
public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1() {
34+
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
35+
assertThat(collection, hasItems("cd", "ef"));
36+
}
37+
38+
@Test
39+
public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect2() {
40+
final List<String> collection = Lists.newArrayList("ab", "cd", "ef");
41+
assertThat(collection, contains("ab", "cd", "ef"));
42+
}
43+
44+
@Test
45+
public final void givenCollectionIsEmpty_whenChecking_thenEmpty() {
46+
final List<String> collection = Lists.newArrayList();
47+
assertThat(collection, empty());
48+
}
49+
50+
@Test
51+
public final void givenCollectionIsNotEmpty_whenChecking_thenNotEmpty() {
52+
final List<String> collection = Lists.newArrayList("a");
53+
assertThat(collection, not(empty()));
54+
}
55+
56+
@Test
57+
public final void givenMapIsEmpty_whenChecking_thenEmpty() {
58+
final Map<String, String> collection = Maps.newHashMap();
59+
assertThat(collection, equalTo(Collections.EMPTY_MAP));
60+
}
61+
62+
@Test
63+
public final void givenArrayIsEmpty_whenChecking_thenEmpty() {
64+
final String[] array = new String[] { "ab" };
65+
assertThat(array, not(emptyArray()));
66+
}
67+
68+
}

0 commit comments

Comments
 (0)