Skip to content

Commit 1fb1a78

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

File tree

1 file changed

+11
-67
lines changed

1 file changed

+11
-67
lines changed

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

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,14 @@ class CategoryServiceImplTest extends Specification {
152152
service.findIdsByNames(names) == []
153153
}
154154

155-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
156155
def 'findIdsByNames() should invoke dao, pass argument and return result from dao'() {
157156
given:
158157
Set<String> expectedNames = Random.setOfStrings()
159158
List<Integer> expectedResult = Random.listOfIntegers()
160159
when:
161160
List<Integer> result = service.findIdsByNames(expectedNames)
162161
then:
163-
1 * categoryDao.findIdsByNames({ Set<String> names ->
164-
assert names == expectedNames
165-
return true
166-
}) >> expectedResult
162+
1 * categoryDao.findIdsByNames(expectedNames) >> expectedResult
167163
and:
168164
result == expectedResult
169165
}
@@ -188,7 +184,6 @@ class CategoryServiceImplTest extends Specification {
188184
thrown IllegalArgumentException
189185
}
190186

191-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
192187
def 'findIdsWhenNameStartsWith() should invoke dao, pass argument and return result from dao'() {
193188
given:
194189
String name = between(1, 10).english()
@@ -198,10 +193,7 @@ class CategoryServiceImplTest extends Specification {
198193
when:
199194
List<Integer> result = service.findIdsWhenNameStartsWith(name)
200195
then:
201-
1 * categoryDao.findIdsByNamePattern({ String pattern ->
202-
assert pattern == expectedPattern
203-
return true
204-
}) >> expectedResult
196+
1 * categoryDao.findIdsByNamePattern(expectedPattern) >> expectedResult
205197
and:
206198
result == expectedResult
207199
}
@@ -226,15 +218,11 @@ class CategoryServiceImplTest extends Specification {
226218
}
227219

228220
@Unroll
229-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
230221
def "findAllAsLinkEntities(String) should pass language '#expectedLanguage' to dao"(String expectedLanguage) {
231222
when:
232223
service.findAllAsLinkEntities(expectedLanguage)
233224
then:
234-
1 * categoryDao.findAllAsLinkEntities({ String language ->
235-
assert language == expectedLanguage
236-
return true
237-
})
225+
1 * categoryDao.findAllAsLinkEntities(expectedLanguage)
238226
where:
239227
expectedLanguage | _
240228
'ru' | _
@@ -245,18 +233,14 @@ class CategoryServiceImplTest extends Specification {
245233
// Tests for findCategoriesWithParents()
246234
//
247235

248-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
249236
def 'findCategoriesWithParents() should invoke dao and return its result'() {
250237
given:
251238
String expectedLang = nullOr(Random.lang())
252239
List<EntityWithParentDto> expectedResult = Random.listOfEntityWithParentDto()
253240
when:
254241
List<EntityWithParentDto> result = service.findCategoriesWithParents(expectedLang)
255242
then:
256-
1 * categoryDao.findCategoriesWithParents({ String lang ->
257-
assert lang == expectedLang
258-
return true
259-
}) >> expectedResult
243+
1 * categoryDao.findCategoriesWithParents(expectedLang) >> expectedResult
260244
and:
261245
result == expectedResult
262246
}
@@ -278,7 +262,6 @@ class CategoryServiceImplTest extends Specification {
278262
null | _
279263
}
280264

281-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
282265
def "findOneAsLinkEntity() should pass arguments to dao"() {
283266
given:
284267
String expectedSlug = 'people'
@@ -289,16 +272,7 @@ class CategoryServiceImplTest extends Specification {
289272
when:
290273
LinkEntityDto actualDto = service.findOneAsLinkEntity(expectedSlug, expectedLang)
291274
then:
292-
1 * categoryDao.findOneAsLinkEntity(
293-
{ String slug ->
294-
assert expectedSlug == slug
295-
return true
296-
},
297-
{ String lang ->
298-
assert expectedLang == lang
299-
return true
300-
}
301-
) >> expectedDto
275+
1 * categoryDao.findOneAsLinkEntity(expectedSlug, expectedLang) >> expectedDto
302276
and:
303277
actualDto == expectedDto
304278
}
@@ -329,17 +303,13 @@ class CategoryServiceImplTest extends Specification {
329303
thrown IllegalArgumentException
330304
}
331305

332-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
333306
def "countCategoriesOf() should pass arguments to dao"() {
334307
given:
335308
Integer expectedCollectionId = 10
336309
when:
337310
service.countCategoriesOf(expectedCollectionId)
338311
then:
339-
1 * categoryDao.countCategoriesOfCollection({ Integer collectionId ->
340-
assert expectedCollectionId == collectionId
341-
return true
342-
}) >> 0L
312+
1 * categoryDao.countCategoriesOfCollection(expectedCollectionId) >> 0L
343313
}
344314

345315
//
@@ -382,15 +352,11 @@ class CategoryServiceImplTest extends Specification {
382352
result == 2L
383353
}
384354

385-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
386355
def "countByName() should pass category name to dao in lowercase"() {
387356
when:
388357
service.countByName('Sport')
389358
then:
390-
1 * categoryDao.countByName({ String name ->
391-
assert name == 'sport'
392-
return true
393-
})
359+
1 * categoryDao.countByName('sport')
394360
}
395361

396362
//
@@ -413,15 +379,11 @@ class CategoryServiceImplTest extends Specification {
413379
result == 2L
414380
}
415381

416-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
417382
def "countByNameRu() should pass category name to dao in lowercase"() {
418383
when:
419384
service.countByNameRu('Спорт')
420385
then:
421-
1 * categoryDao.countByNameRu({ String name ->
422-
assert name == 'спорт'
423-
return true
424-
})
386+
1 * categoryDao.countByNameRu('спорт')
425387
}
426388

427389
//
@@ -435,7 +397,6 @@ class CategoryServiceImplTest extends Specification {
435397
thrown IllegalArgumentException
436398
}
437399

438-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
439400
def "countAddedSince() should invoke dao, pass argument and return result from dao"() {
440401
given:
441402
Date expectedDate = new Date()
@@ -444,10 +405,7 @@ class CategoryServiceImplTest extends Specification {
444405
when:
445406
long result = service.countAddedSince(expectedDate)
446407
then:
447-
1 * categoryDao.countAddedSince({ Date date ->
448-
assert date == expectedDate
449-
return true
450-
}) >> expectedResult
408+
1 * categoryDao.countAddedSince(expectedDate) >> expectedResult
451409
and:
452410
result == expectedResult
453411
}
@@ -463,7 +421,6 @@ class CategoryServiceImplTest extends Specification {
463421
thrown IllegalArgumentException
464422
}
465423

466-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
467424
def "countUntranslatedNamesSince() should invoke dao, pass argument and return result from dao"() {
468425
given:
469426
Date expectedDate = new Date()
@@ -472,10 +429,7 @@ class CategoryServiceImplTest extends Specification {
472429
when:
473430
long result = service.countUntranslatedNamesSince(expectedDate)
474431
then:
475-
1 * categoryDao.countUntranslatedNamesSince({ Date date ->
476-
assert date == expectedDate
477-
return true
478-
}) >> expectedResult
432+
1 * categoryDao.countUntranslatedNamesSince(expectedDate) >> expectedResult
479433
and:
480434
result == expectedResult
481435
}
@@ -491,7 +445,6 @@ class CategoryServiceImplTest extends Specification {
491445
thrown IllegalArgumentException
492446
}
493447

494-
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
495448
def "getStatisticsOf() should pass arguments to dao"() {
496449
given:
497450
Integer expectedCollectionId = 15
@@ -500,16 +453,7 @@ class CategoryServiceImplTest extends Specification {
500453
when:
501454
service.getStatisticsOf(expectedCollectionId, expectedLang)
502455
then:
503-
1 * categoryDao.getStatisticsOf(
504-
{ Integer collectionId ->
505-
assert expectedCollectionId == collectionId
506-
return true
507-
},
508-
{ String lang ->
509-
assert expectedLang == lang
510-
return true
511-
}
512-
) >> null
456+
1 * categoryDao.getStatisticsOf(expectedCollectionId, expectedLang) >> null
513457
}
514458

515459
}

0 commit comments

Comments
 (0)