25
25
import java .util .List ;
26
26
import java .util .function .Consumer ;
27
27
28
- import org .assertj .core .api .AssertionInfo ;
29
- import org .assertj .core .internal .Iterables ;
30
28
import org .assertj .core .internal .IterablesBaseTest ;
31
- import org .junit . jupiter . api . DisplayName ;
29
+ import org .assertj . core . test . Jedi ;
32
30
import org .junit .jupiter .api .Test ;
33
31
34
32
/**
35
- * Tests for <code>{@link Iterables#assertSatisfiesExactlyInAnyOrder(AssertionInfo, Iterable, Consumer[])}</code>.
36
- *
37
33
* @author Ting Sun
38
34
*/
39
- @ DisplayName ("Iterables assertSatisfiesExactlyInAnyOrder" )
40
35
class Iterables_assertSatisfiesExactlyInAnyOrder_Test extends IterablesBaseTest {
41
36
42
37
private List <String > actual = newArrayList ("Luke" , "Leia" , "Yoda" );
@@ -50,7 +45,6 @@ void should_pass_if_all_consumers_are_satisfied_by_different_elements_in_order()
50
45
assertThat (s ).hasSize (4 );
51
46
assertThat (s ).doesNotContain ("L" );
52
47
}; // Matches "Yoda"
53
-
54
48
// WHEN/THEN
55
49
iterables .assertSatisfiesExactlyInAnyOrder (info , actual , array (consumer1 , consumer2 , consumer3 ));
56
50
}
@@ -61,7 +55,6 @@ void should_pass_if_all_consumers_are_satisfied_by_different_elements_in_any_ord
61
55
Consumer <String > consumer1 = s -> assertThat (s ).contains ("Y" ); // Matches "Yoda"
62
56
Consumer <String > consumer2 = s -> assertThat (s ).contains ("L" ); // Matches "Luke" and "Leia"
63
57
Consumer <String > consumer3 = s -> assertThat (s ).doesNotContain ("a" ); // Matches "Luke"
64
-
65
58
// WHEN/THEN
66
59
iterables .assertSatisfiesExactlyInAnyOrder (info , actual , array (consumer1 , consumer2 , consumer3 ));
67
60
iterables .assertSatisfiesExactlyInAnyOrder (info , actual , array (consumer1 , consumer3 , consumer2 ));
@@ -82,7 +75,6 @@ void should_fail_if_one_of_the_consumer_cannot_be_satisfied() {
82
75
array (consumer1 ,
83
76
consumer2 ,
84
77
consumer3 )));
85
-
86
78
// THEN
87
79
then (assertionError ).hasMessage (shouldSatisfyExactlyInAnyOrder (actual ).create ());
88
80
}
@@ -118,12 +110,12 @@ void should_fail_if_one_of_the_requirements_cannot_be_satisfied() {
118
110
}
119
111
120
112
@ Test
121
- void should_pass_if_iterable_contains_multiple_equal_elements () {
113
+ void should_pass_if_iterable_contains_same_elements () {
122
114
// GIVEN
123
- List <String > names = newArrayList ("Luke" , "Luke" );
115
+ String luke = "Luke" ;
116
+ List <String > names = newArrayList (luke , luke );
124
117
Consumer <String > consumer1 = s -> assertThat (s ).contains ("L" );
125
118
Consumer <String > consumer2 = s -> assertThat (s ).contains ("u" );
126
-
127
119
// WHEN/THEN
128
120
iterables .assertSatisfiesExactlyInAnyOrder (info , names , array (consumer1 , consumer2 ));
129
121
}
@@ -186,4 +178,38 @@ void should_fail_if_there_are_too_many_consumers() {
186
178
then (assertionError ).hasMessage (shouldHaveSize (actual , 3 , 4 ).create ());
187
179
}
188
180
181
+ @ Test
182
+ void should_pass_without_relying_on_elements_equality () {
183
+ // GIVEN
184
+ List <Jedi > actual = newArrayList (new JediOverridingEquals ("Luke" , "blue" ),
185
+ new JediOverridingEquals ("Luke" , "green" ),
186
+ new JediOverridingEquals ("Luke" , "green" ));
187
+ Consumer <Jedi >[] consumers = array (jedi -> assertThat (jedi .lightSaberColor ).isEqualTo ("green" ),
188
+ jedi -> assertThat (jedi .lightSaberColor ).isEqualTo ("blue" ),
189
+ jedi -> assertThat (jedi .lightSaberColor ).isEqualTo ("green" ));
190
+ // WHEN/THEN
191
+ iterables .assertSatisfiesExactlyInAnyOrder (info , actual , consumers );
192
+ }
193
+
194
+ private static class JediOverridingEquals extends Jedi {
195
+
196
+ private JediOverridingEquals (String name , String lightSaberColor ) {
197
+ super (name , lightSaberColor );
198
+ }
199
+
200
+ @ Override
201
+ public boolean equals (Object o ) {
202
+ if (this == o ) return true ;
203
+ if (o == null || getClass () != o .getClass ()) return false ;
204
+ JediOverridingEquals jedi = (JediOverridingEquals ) o ;
205
+ return getName ().equals (jedi .getName ());
206
+ }
207
+
208
+ @ Override
209
+ public int hashCode () {
210
+ return getName ().hashCode ();
211
+ }
212
+
213
+ }
214
+
189
215
}
0 commit comments