15
15
*/
16
16
package org .springframework .data .mongodb .repository .support ;
17
17
18
- import static org .hamcrest .Matchers .*;
19
- import static org .junit .Assert .*;
18
+ import static org .assertj .core .api .Assertions .*;
20
19
import static org .springframework .data .domain .ExampleMatcher .*;
21
20
22
21
import java .util .ArrayList ;
29
28
import java .util .Set ;
30
29
import java .util .UUID ;
31
30
32
- import org .assertj .core .api .Assertions ;
33
31
import org .junit .Before ;
34
32
import org .junit .Test ;
35
33
import org .junit .runner .RunWith ;
36
34
import org .springframework .beans .factory .annotation .Autowired ;
37
35
import org .springframework .data .domain .Example ;
38
- import org .springframework .data .domain .ExampleMatcher .StringMatcher ;
39
36
import org .springframework .data .domain .Page ;
40
37
import org .springframework .data .domain .PageRequest ;
38
+ import org .springframework .data .domain .ExampleMatcher .*;
41
39
import org .springframework .data .geo .Point ;
42
40
import org .springframework .data .mongodb .core .MongoTemplate ;
43
41
import org .springframework .data .mongodb .core .geo .GeoJsonPoint ;
@@ -87,32 +85,28 @@ public void setUp() {
87
85
88
86
@ Test
89
87
public void findALlFromCustomCollectionName () {
90
- List <Person > result = repository .findAll ();
91
- assertThat (result , hasSize (all .size ()));
88
+ assertThat (repository .findAll ()).hasSize (all .size ());
92
89
}
93
90
94
91
@ Test
95
92
public void findOneFromCustomCollectionName () {
96
- Person result = repository .findById (dave .getId ()).get ();
97
- assertThat (result , is (dave ));
93
+ assertThat (repository .findById (dave .getId ()).get ()).isEqualTo (dave );
98
94
}
99
95
100
96
@ Test
101
97
public void deleteFromCustomCollectionName () {
98
+
102
99
repository .delete (dave );
103
- List <Person > result = repository .findAll ();
104
100
105
- assertThat (result , hasSize (all .size () - 1 ));
106
- assertThat (result , not (hasItem (dave )));
101
+ assertThat (repository .findAll ()).hasSize (all .size () - 1 ).doesNotContain (dave );
107
102
}
108
103
109
104
@ Test
110
105
public void deleteByIdFromCustomCollectionName () {
106
+
111
107
repository .deleteById (dave .getId ());
112
- List <Person > result = repository .findAll ();
113
108
114
- assertThat (result , hasSize (all .size () - 1 ));
115
- assertThat (result , not (hasItem (dave )));
109
+ assertThat (repository .findAll ()).hasSize (all .size () - 1 ).doesNotContain (dave );
116
110
}
117
111
118
112
@ Test // DATAMONGO-1054
@@ -123,9 +117,7 @@ public void shouldInsertSingle() {
123
117
Person person1 = new Person ("First1" + randomId , "Last2" + randomId , 42 );
124
118
person1 = repository .insert (person1 );
125
119
126
- Person saved = repository .findById (person1 .getId ()).get ();
127
-
128
- assertThat (saved , is (equalTo (person1 )));
120
+ assertThat (repository .findById (person1 .getId ())).contains (person1 );
129
121
}
130
122
131
123
@ Test // DATAMONGO-1054
@@ -143,7 +135,7 @@ public void shouldInsertMultipleFromList() {
143
135
144
136
List <Person > saved = repository .insert (persons );
145
137
146
- assertThat (saved , hasSize (persons .size () ));
138
+ assertThat (saved ). hasSize (persons .size ());
147
139
assertThatAllReferencePersonsWereStoredCorrectly (idToPerson , saved );
148
140
}
149
141
@@ -162,7 +154,7 @@ public void shouldInsertMutlipleFromSet() {
162
154
163
155
List <Person > saved = repository .insert (persons );
164
156
165
- assertThat (saved , hasSize (persons .size () ));
157
+ assertThat (saved ). hasSize (persons .size ());
166
158
assertThatAllReferencePersonsWereStoredCorrectly (idToPerson , saved );
167
159
}
168
160
@@ -175,9 +167,8 @@ public void findByExampleShouldLookUpEntriesCorrectly() {
175
167
176
168
Page <Person > result = repository .findAll (Example .of (sample ), PageRequest .of (0 , 10 ));
177
169
178
- assertThat (result .getContent (), hasItems (dave , oliver ));
179
- assertThat (result .getContent (), hasSize (2 ));
180
- assertThat (result .getTotalPages (), is (1 ));
170
+ assertThat (result .getContent ()).hasSize (2 ).contains (dave , oliver );
171
+ assertThat (result .getTotalPages ()).isEqualTo (1 );
181
172
}
182
173
183
174
@ Test // DATAMONGO-1464
@@ -189,8 +180,8 @@ public void findByExampleMultiplePagesShouldLookUpEntriesCorrectly() {
189
180
190
181
Page <Person > result = repository .findAll (Example .of (sample ), PageRequest .of (0 , 1 ));
191
182
192
- assertThat (result .getContent (), hasSize (1 ) );
193
- assertThat (result .getTotalPages (), is ( 2 ) );
183
+ assertThat (result .getContent ()). hasSize (1 );
184
+ assertThat (result .getTotalPages ()). isEqualTo ( 2 );
194
185
}
195
186
196
187
@ Test // DATAMONGO-1245
@@ -200,10 +191,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectly() {
200
191
sample .setLastname ("Matthews" );
201
192
trimDomainType (sample , "id" , "createdAt" , "email" );
202
193
203
- List <Person > result = repository .findAll (Example .of (sample ));
204
-
205
- assertThat (result , containsInAnyOrder (dave , oliver ));
206
- assertThat (result , hasSize (2 ));
194
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (2 ).contains (dave , oliver );
207
195
}
208
196
209
197
@ Test // DATAMONGO-1245
@@ -219,10 +207,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObject()
219
207
sample .setAddress (dave .getAddress ());
220
208
trimDomainType (sample , "id" , "createdAt" , "email" );
221
209
222
- List <Person > result = repository .findAll (Example .of (sample ));
223
-
224
- assertThat (result , hasItem (dave ));
225
- assertThat (result , hasSize (1 ));
210
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (1 ).contains (dave );
226
211
}
227
212
228
213
@ Test // DATAMONGO-1245
@@ -238,10 +223,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingPartialNestedOb
238
223
sample .setAddress (new Address (null , null , "Washington" ));
239
224
trimDomainType (sample , "id" , "createdAt" , "email" );
240
225
241
- List <Person > result = repository .findAll (Example .of (sample ));
242
-
243
- assertThat (result , hasItems (dave , oliver ));
244
- assertThat (result , hasSize (2 ));
226
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (2 ).contains (dave , oliver );
245
227
}
246
228
247
229
@ Test // DATAMONGO-1245
@@ -255,9 +237,8 @@ public void findAllByExampleShouldNotFindEntriesWhenUsingPartialNestedObjectInSt
255
237
trimDomainType (sample , "id" , "createdAt" , "email" );
256
238
257
239
Example <Person > example = Example .of (sample , matching ().withIncludeNullValues ());
258
- List <Person > result = repository .findAll (example );
259
240
260
- assertThat (result , empty () );
241
+ assertThat (repository . findAll ( example )). isEmpty ( );
261
242
}
262
243
263
244
@ Test // DATAMONGO-1245
@@ -271,10 +252,8 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObjectInS
271
252
trimDomainType (sample , "id" , "createdAt" , "email" );
272
253
273
254
Example <Person > example = Example .of (sample , matching ().withIncludeNullValues ());
274
- List <Person > result = repository .findAll (example );
275
255
276
- assertThat (result , hasItem (dave ));
277
- assertThat (result , hasSize (1 ));
256
+ assertThat (repository .findAll (example )).hasSize (1 ).contains (dave );
278
257
}
279
258
280
259
@ Test // DATAMONGO-1245
@@ -285,10 +264,8 @@ public void findAllByExampleShouldRespectStringMatchMode() {
285
264
trimDomainType (sample , "id" , "createdAt" , "email" );
286
265
287
266
Example <Person > example = Example .of (sample , matching ().withStringMatcher (StringMatcher .STARTING ));
288
- List <Person > result = repository .findAll (example );
289
267
290
- assertThat (result , hasItems (dave , oliver ));
291
- assertThat (result , hasSize (2 ));
268
+ assertThat (repository .findAll (example )).hasSize (2 ).contains (dave , oliver );
292
269
}
293
270
294
271
@ Test // DATAMONGO-1245
@@ -308,10 +285,7 @@ public void findAllByExampleShouldResolveDbRefCorrectly() {
308
285
sample .setCreator (user );
309
286
trimDomainType (sample , "id" , "createdAt" , "email" );
310
287
311
- List <Person > result = repository .findAll (Example .of (sample ));
312
-
313
- assertThat (result , hasItem (megan ));
314
- assertThat (result , hasSize (1 ));
288
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (1 ).contains (megan );
315
289
}
316
290
317
291
@ Test // DATAMONGO-1245
@@ -326,10 +300,7 @@ public void findAllByExampleShouldResolveLegacyCoordinatesCorrectly() {
326
300
sample .setLocation (megan .getLocation ());
327
301
trimDomainType (sample , "id" , "createdAt" , "email" );
328
302
329
- List <Person > result = repository .findAll (Example .of (sample ));
330
-
331
- assertThat (result , hasItem (megan ));
332
- assertThat (result , hasSize (1 ));
303
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (1 ).contains (megan );
333
304
}
334
305
335
306
@ Test // DATAMONGO-1245
@@ -344,10 +315,7 @@ public void findAllByExampleShouldResolveGeoJsonCoordinatesCorrectly() {
344
315
sample .setLocation (megan .getLocation ());
345
316
trimDomainType (sample , "id" , "createdAt" , "email" );
346
317
347
- List <Person > result = repository .findAll (Example .of (sample ));
348
-
349
- assertThat (result , hasItem (megan ));
350
- assertThat (result , hasSize (1 ));
318
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (1 ).contains (megan );
351
319
}
352
320
353
321
@ Test // DATAMONGO-1245
@@ -363,10 +331,7 @@ public void findAllByExampleShouldProcessInheritanceCorrectly() {
363
331
364
332
trimDomainType (sample , "id" , "createdAt" , "email" );
365
333
366
- List <PersonExtended > result = repository .findAll (Example .of (sample ));
367
-
368
- assertThat (result , hasSize (1 ));
369
- assertThat (result , hasItem (reference ));
334
+ assertThat (repository .findAll (Example .of (sample ))).hasSize (1 ).contains (reference );
370
335
}
371
336
372
337
@ Test // DATAMONGO-1245
@@ -377,9 +342,7 @@ public void findOneByExampleShouldLookUpEntriesCorrectly() {
377
342
sample .setLastname ("Matthews" );
378
343
trimDomainType (sample , "id" , "createdAt" , "email" );
379
344
380
- Optional <Person > result = repository .findOne (Example .of (sample ));
381
-
382
- Assertions .assertThat (result ).isPresent ().contains (dave );
345
+ assertThat (repository .findOne (Example .of (sample ))).isPresent ().contains (dave );
383
346
}
384
347
385
348
@ Test // DATAMONGO-1245
@@ -390,9 +353,7 @@ public void existsByExampleShouldLookUpEntriesCorrectly() {
390
353
sample .setLastname ("Matthews" );
391
354
trimDomainType (sample , "id" , "createdAt" , "email" );
392
355
393
- boolean result = repository .exists (Example .of (sample ));
394
-
395
- assertThat (result , is (true ));
356
+ assertThat (repository .exists (Example .of (sample ))).isTrue ();
396
357
}
397
358
398
359
@ Test // DATAMONGO-1245
@@ -402,16 +363,14 @@ public void countByExampleShouldLookUpEntriesCorrectly() {
402
363
sample .setLastname ("Matthews" );
403
364
trimDomainType (sample , "id" , "createdAt" , "email" );
404
365
405
- long result = repository .count (Example .of (sample ));
406
-
407
- assertThat (result , is (equalTo (2L )));
366
+ assertThat (repository .count (Example .of (sample ))).isEqualTo (2L );
408
367
}
409
368
410
369
private void assertThatAllReferencePersonsWereStoredCorrectly (Map <String , Person > references , List <Person > saved ) {
411
370
412
371
for (Person person : saved ) {
413
372
Person reference = references .get (person .getId ());
414
- assertThat (person , is ( equalTo ( reference )) );
373
+ assertThat (person ). isEqualTo ( reference );
415
374
}
416
375
}
417
376
0 commit comments