Skip to content

Commit 050817f

Browse files
committed
CountryServiceImplTest: simplify code by replacing closures with expected values.
No functional changes.
1 parent 55cf0f1 commit 050817f

File tree

1 file changed

+13
-77
lines changed

1 file changed

+13
-77
lines changed

src/test/groovy/ru/mystamps/web/service/CountryServiceImplTest.groovy

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,14 @@ class CountryServiceImplTest extends Specification {
151151
service.findIdsByNames(names) == []
152152
}
153153

154-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
155154
def 'findIdsByNames() should invoke dao, pass argument and return result from dao'() {
156155
given:
157156
Set<String> expectedNames = Random.setOfStrings()
158157
List<Integer> expectedResult = Random.listOfIntegers()
159158
when:
160159
List<Integer> result = service.findIdsByNames(expectedNames)
161160
then:
162-
1 * countryDao.findIdsByNames({ Set<String> names ->
163-
assert names == expectedNames
164-
return true
165-
}) >> expectedResult
161+
1 * countryDao.findIdsByNames(expectedNames) >> expectedResult
166162
and:
167163
result == expectedResult
168164
}
@@ -187,7 +183,6 @@ class CountryServiceImplTest extends Specification {
187183
thrown IllegalArgumentException
188184
}
189185

190-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
191186
def 'findIdsWhenNameStartsWith() should invoke dao, pass argument and return result from dao'() {
192187
given:
193188
String name = between(1, 10).english()
@@ -197,10 +192,7 @@ class CountryServiceImplTest extends Specification {
197192
when:
198193
List<Integer> result = service.findIdsWhenNameStartsWith(name)
199194
then:
200-
1 * countryDao.findIdsByNamePattern({ String pattern ->
201-
assert pattern == expectedPattern
202-
return true
203-
}) >> expectedResult
195+
1 * countryDao.findIdsByNamePattern(expectedPattern) >> expectedResult
204196
and:
205197
result == expectedResult
206198
}
@@ -225,15 +217,11 @@ class CountryServiceImplTest extends Specification {
225217
}
226218

227219
@Unroll
228-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
229220
def "findAllAsLinkEntities(String) should pass language '#expectedLanguage' to dao"(String expectedLanguage) {
230221
when:
231222
service.findAllAsLinkEntities(expectedLanguage)
232223
then:
233-
1 * countryDao.findAllAsLinkEntities({ String language ->
234-
assert language == expectedLanguage
235-
return true
236-
})
224+
1 * countryDao.findAllAsLinkEntities(expectedLanguage)
237225
where:
238226
expectedLanguage | _
239227
'ru' | _
@@ -256,7 +244,6 @@ class CountryServiceImplTest extends Specification {
256244
null | _
257245
}
258246

259-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
260247
def "findOneAsLinkEntity() should pass arguments to dao"() {
261248
given:
262249
String expectedSlug = 'france'
@@ -267,16 +254,7 @@ class CountryServiceImplTest extends Specification {
267254
when:
268255
LinkEntityDto actualDto = service.findOneAsLinkEntity(expectedSlug, expectedLang)
269256
then:
270-
1 * countryDao.findOneAsLinkEntity(
271-
{ String countrySlug ->
272-
assert expectedSlug == countrySlug
273-
return true
274-
},
275-
{ String lang ->
276-
assert expectedLang == lang
277-
return true
278-
}
279-
) >> expectedDto
257+
1 * countryDao.findOneAsLinkEntity(expectedSlug, expectedLang) >> expectedDto
280258
and:
281259
actualDto == expectedDto
282260
}
@@ -307,17 +285,13 @@ class CountryServiceImplTest extends Specification {
307285
thrown IllegalArgumentException
308286
}
309287

310-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
311288
def "countCountriesOf() should pass arguments to dao"() {
312289
given:
313290
Integer expectedCollectionId = 9
314291
when:
315292
service.countCountriesOf(expectedCollectionId)
316293
then:
317-
1 * countryDao.countCountriesOfCollection({ Integer collectionId ->
318-
assert expectedCollectionId == collectionId
319-
return true
320-
}) >> 0L
294+
1 * countryDao.countCountriesOfCollection(expectedCollectionId) >> 0L
321295
}
322296

323297
//
@@ -360,15 +334,11 @@ class CountryServiceImplTest extends Specification {
360334
result == 2L
361335
}
362336

363-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
364337
def "countByName() should pass country name to dao in lowercase"() {
365338
when:
366339
service.countByName('Canada')
367340
then:
368-
1 * countryDao.countByName({ String name ->
369-
assert name == 'canada'
370-
return true
371-
})
341+
1 * countryDao.countByName('canada')
372342
}
373343

374344
//
@@ -391,15 +361,11 @@ class CountryServiceImplTest extends Specification {
391361
result == 2L
392362
}
393363

394-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
395364
def "countByNameRu() should pass category name to dao in lowercase"() {
396365
when:
397366
service.countByNameRu('Канада')
398367
then:
399-
1 * countryDao.countByNameRu({ String name ->
400-
assert name == 'канада'
401-
return true
402-
})
368+
1 * countryDao.countByNameRu('канада')
403369
}
404370

405371
//
@@ -413,7 +379,6 @@ class CountryServiceImplTest extends Specification {
413379
thrown IllegalArgumentException
414380
}
415381

416-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
417382
def "countAddedSince() should invoke dao, pass argument and return result from dao"() {
418383
given:
419384
Date expectedDate = new Date()
@@ -422,10 +387,7 @@ class CountryServiceImplTest extends Specification {
422387
when:
423388
long result = service.countAddedSince(expectedDate)
424389
then:
425-
1 * countryDao.countAddedSince({ Date date ->
426-
assert date == expectedDate
427-
return true
428-
}) >> expectedResult
390+
1 * countryDao.countAddedSince(expectedDate) >> expectedResult
429391
and:
430392
result == expectedResult
431393
}
@@ -441,7 +403,6 @@ class CountryServiceImplTest extends Specification {
441403
thrown IllegalArgumentException
442404
}
443405

444-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
445406
def "countUntranslatedNamesSince() should invoke dao, pass argument and return result from dao"() {
446407
given:
447408
Date expectedDate = new Date()
@@ -450,10 +411,7 @@ class CountryServiceImplTest extends Specification {
450411
when:
451412
long result = service.countUntranslatedNamesSince(expectedDate)
452413
then:
453-
1 * countryDao.countUntranslatedNamesSince({ Date date ->
454-
assert date == expectedDate
455-
return true
456-
}) >> expectedResult
414+
1 * countryDao.countUntranslatedNamesSince(expectedDate) >> expectedResult
457415
and:
458416
result == expectedResult
459417
}
@@ -469,7 +427,6 @@ class CountryServiceImplTest extends Specification {
469427
thrown IllegalArgumentException
470428
}
471429

472-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
473430
def "getStatisticsOf() should pass arguments to dao"() {
474431
given:
475432
Integer expectedCollectionId = 17
@@ -478,16 +435,7 @@ class CountryServiceImplTest extends Specification {
478435
when:
479436
service.getStatisticsOf(expectedCollectionId, expectedLang)
480437
then:
481-
1 * countryDao.getStatisticsOf(
482-
{ Integer collectionId ->
483-
assert expectedCollectionId == collectionId
484-
return true
485-
},
486-
{ String lang ->
487-
assert expectedLang == lang
488-
return true
489-
}
490-
) >> null
438+
1 * countryDao.getStatisticsOf(expectedCollectionId, expectedLang) >> null
491439
}
492440

493441
//
@@ -501,50 +449,38 @@ class CountryServiceImplTest extends Specification {
501449
thrown IllegalArgumentException
502450
}
503451

504-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
505452
def 'suggestCountryForUser() should return country of the last created series'() {
506453
given:
507454
Integer expectedUserId = 18
508455
String expectedSlug = 'brazil'
509456
when:
510457
String slug = service.suggestCountryForUser(expectedUserId)
511458
then:
512-
1 * countryDao.findCountryOfLastCreatedSeriesByUser({ Integer userId ->
513-
assert expectedUserId == userId
514-
return true
515-
}) >> expectedSlug
459+
1 * countryDao.findCountryOfLastCreatedSeriesByUser(expectedUserId) >> expectedSlug
516460
and:
517461
slug == expectedSlug
518462
}
519463

520-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
521464
def 'suggestCountryForUser() should return popular country from collection'() {
522465
given:
523466
Integer expectedUserId = 19
524467
String expectedSlug = 'mexica'
525468
when:
526469
String slug = service.suggestCountryForUser(expectedUserId)
527470
then:
528-
1 * countryDao.findPopularCountryInCollection({ Integer userId ->
529-
assert expectedUserId == userId
530-
return true
531-
}) >> expectedSlug
471+
1 * countryDao.findPopularCountryInCollection(expectedUserId) >> expectedSlug
532472
and:
533473
slug == expectedSlug
534474
}
535475

536-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
537476
def 'suggestCountryForUser() should return a recently created country'() {
538477
given:
539478
Integer expectedUserId = 21
540479
String expectedSlug = 'spain'
541480
when:
542481
String slug = service.suggestCountryForUser(expectedUserId)
543482
then:
544-
1 * countryDao.findLastCountryCreatedByUser({ Integer userId ->
545-
assert expectedUserId == userId
546-
return true
547-
}) >> expectedSlug
483+
1 * countryDao.findLastCountryCreatedByUser(expectedUserId) >> expectedSlug
548484
and:
549485
slug == expectedSlug
550486
}

0 commit comments

Comments
 (0)