11package org .baeldung .hamcrest ;
22
33import static org .hamcrest .Matchers .contains ;
4+ import static org .hamcrest .Matchers .containsInAnyOrder ;
45import static org .hamcrest .Matchers .empty ;
56import static org .hamcrest .Matchers .emptyArray ;
67import static org .hamcrest .Matchers .equalTo ;
8+ import static org .hamcrest .Matchers .everyItem ;
9+ import static org .hamcrest .Matchers .greaterThan ;
710import static org .hamcrest .Matchers .hasItem ;
811import static org .hamcrest .Matchers .hasItems ;
12+ import static org .hamcrest .Matchers .hasSize ;
913import static org .hamcrest .Matchers .not ;
1014import static org .junit .Assert .assertThat ;
1115
1216import java .util .Collections ;
1317import java .util .List ;
1418import java .util .Map ;
1519
20+ import org .hamcrest .Matchers ;
1621import org .junit .Test ;
1722
1823import com .google .common .collect .Lists ;
@@ -32,15 +37,21 @@ public final void whenVerifyingSingleElementIsPartOfCollection_thenCorrect() {
3237 @ Test
3338 public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1 () {
3439 final List <String > collection = Lists .newArrayList ("ab" , "cd" , "ef" );
35- assertThat (collection , hasItems ("cd " , "ef " ));
40+ assertThat (collection , hasItems ("ef " , "cd " ));
3641 }
3742
3843 @ Test
39- public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect2 () {
44+ public final void whenVerifyingMultipleElementsArePartOfCollectionInStrictOrder_thenCorrect2 () {
4045 final List <String > collection = Lists .newArrayList ("ab" , "cd" , "ef" );
4146 assertThat (collection , contains ("ab" , "cd" , "ef" ));
4247 }
4348
49+ @ Test
50+ public final void whenVerifyingMultipleElementsArePartOfCollectionInAnyOrder_thenCorrect2 () {
51+ final List <String > collection = Lists .newArrayList ("ab" , "cd" , "ef" );
52+ assertThat (collection , containsInAnyOrder ("cd" , "ab" , "ef" ));
53+ }
54+
4455 @ Test
4556 public final void givenCollectionIsEmpty_whenChecking_thenEmpty () {
4657 final List <String > collection = Lists .newArrayList ();
@@ -65,4 +76,22 @@ public final void givenArrayIsEmpty_whenChecking_thenEmpty() {
6576 assertThat (array , not (emptyArray ()));
6677 }
6778
79+ @ Test
80+ public final void whenCollectionSizeIsChecked_thenCorrect () {
81+ final List <String > collection = Lists .newArrayList ("ab" , "cd" , "ef" );
82+ assertThat (collection , hasSize (3 ));
83+ }
84+
85+ @ Test
86+ public final void whenIterableSizeIsChecked_thenCorrect () {
87+ final Iterable <String > collection = Lists .newArrayList ("ab" , "cd" , "ef" );
88+ assertThat (collection , Matchers .<String > iterableWithSize (3 ));
89+ }
90+
91+ @ Test
92+ public final void whenCheckingConditionOverEachItem_thenCorrect () {
93+ final List <Integer > collection = Lists .newArrayList (15 , 20 , 25 , 30 );
94+ assertThat (collection , everyItem (greaterThan (10 )));
95+ }
96+
6897}
0 commit comments