Skip to content

Commit ddc3759

Browse files
committed
refactor(tests): check not only a presence of an exception itself but also its message.
Addressed to #925 No functional changes.
1 parent d420271 commit ddc3759

File tree

2 files changed

+79
-34
lines changed

2 files changed

+79
-34
lines changed

src/test/groovy/ru/mystamps/web/feature/category/CategoryServiceImplTest.groovy

+39-17
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class CategoryServiceImplTest extends Specification {
5656
when:
5757
service.add(null, Random.userId())
5858
then:
59-
thrown IllegalArgumentException
59+
IllegalArgumentException ex = thrown()
60+
ex.message == 'DTO must be non null'
6061
}
6162

6263
def "add() should throw exception when English category name is null"() {
@@ -65,14 +66,16 @@ class CategoryServiceImplTest extends Specification {
6566
when:
6667
service.add(form, Random.userId())
6768
then:
68-
thrown IllegalArgumentException
69+
IllegalArgumentException ex = thrown()
70+
ex.message == 'Category name in English must be non null'
6971
}
7072

7173
def "add() should throw exception when user is null"() {
7274
when:
7375
service.add(form, null)
7476
then:
75-
thrown IllegalArgumentException
77+
IllegalArgumentException ex = thrown()
78+
ex.message == 'User id must be non null'
7679
}
7780

7881
def "add() should call dao"() {
@@ -92,11 +95,12 @@ class CategoryServiceImplTest extends Specification {
9295

9396
def "add() should throw exception when name can't be converted to slug"() {
9497
given:
95-
form.setName(null)
98+
form.setName('-')
9699
when:
97100
service.add(form, Random.userId())
98101
then:
99-
thrown IllegalArgumentException
102+
IllegalArgumentException ex = thrown()
103+
ex.message == "Slug for string '-' must be non empty"
100104
}
101105

102106
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -170,7 +174,8 @@ class CategoryServiceImplTest extends Specification {
170174
when:
171175
service.findIdsWhenNameStartsWith(nullOrBlank())
172176
then:
173-
thrown IllegalArgumentException
177+
IllegalArgumentException ex = thrown()
178+
ex.message == 'Name must be non-blank'
174179
}
175180

176181
def 'findIdsWhenNameStartsWith() should throw exception when name contains percent or underscore character'() {
@@ -179,7 +184,8 @@ class CategoryServiceImplTest extends Specification {
179184
when:
180185
service.findIdsWhenNameStartsWith(invalidName)
181186
then:
182-
thrown IllegalArgumentException
187+
IllegalArgumentException ex = thrown()
188+
ex.message == "Name must not contain '%' or '_' chars"
183189
}
184190

185191
def 'findIdsWhenNameStartsWith() should invoke dao, pass argument and return result from dao'() {
@@ -247,17 +253,25 @@ class CategoryServiceImplTest extends Specification {
247253
// Tests for findOneAsLinkEntity()
248254
//
249255

256+
def 'findOneAsLinkEntity() should throw exception when category slug is null'() {
257+
when:
258+
service.findOneAsLinkEntity(null, Random.lang())
259+
then:
260+
IllegalArgumentException ex = thrown()
261+
ex.message == 'Category slug must be non null'
262+
}
263+
250264
@Unroll
251265
def "findOneAsLinkEntity() should throw exception when category slug is '#slug'"(String slug) {
252266
when:
253267
service.findOneAsLinkEntity(slug, 'ru')
254268
then:
255-
thrown IllegalArgumentException
269+
IllegalArgumentException ex = thrown()
270+
ex.message == 'Category slug must be non empty'
256271
where:
257272
slug | _
258273
' ' | _
259274
'' | _
260-
null | _
261275
}
262276

263277
def "findOneAsLinkEntity() should pass arguments to dao"() {
@@ -298,7 +312,8 @@ class CategoryServiceImplTest extends Specification {
298312
when:
299313
service.countCategoriesOf(null)
300314
then:
301-
thrown IllegalArgumentException
315+
IllegalArgumentException ex = thrown()
316+
ex.message == 'Collection id must be non null'
302317
}
303318

304319
def "countCategoriesOf() should pass arguments to dao"() {
@@ -318,7 +333,8 @@ class CategoryServiceImplTest extends Specification {
318333
when:
319334
service.countBySlug(null)
320335
then:
321-
thrown IllegalArgumentException
336+
IllegalArgumentException ex = thrown()
337+
ex.message == 'Category slug must be non null'
322338
}
323339

324340
def "countBySlug() should call dao"() {
@@ -338,7 +354,8 @@ class CategoryServiceImplTest extends Specification {
338354
when:
339355
service.countByName(null)
340356
then:
341-
thrown IllegalArgumentException
357+
IllegalArgumentException ex = thrown()
358+
ex.message == 'Name must be non null'
342359
}
343360

344361
def "countByName() should call dao"() {
@@ -365,7 +382,8 @@ class CategoryServiceImplTest extends Specification {
365382
when:
366383
service.countByNameRu(null)
367384
then:
368-
thrown IllegalArgumentException
385+
IllegalArgumentException ex = thrown()
386+
ex.message == 'Name in Russian must be non null'
369387
}
370388

371389
def "countByNameRu() should call dao"() {
@@ -392,7 +410,8 @@ class CategoryServiceImplTest extends Specification {
392410
when:
393411
service.countAddedSince(null)
394412
then:
395-
thrown IllegalArgumentException
413+
IllegalArgumentException ex = thrown()
414+
ex.message == 'Date must be non null'
396415
}
397416

398417
def "countAddedSince() should invoke dao, pass argument and return result from dao"() {
@@ -416,7 +435,8 @@ class CategoryServiceImplTest extends Specification {
416435
when:
417436
service.countUntranslatedNamesSince(null)
418437
then:
419-
thrown IllegalArgumentException
438+
IllegalArgumentException ex = thrown()
439+
ex.message == 'Date must be non null'
420440
}
421441

422442
def "countUntranslatedNamesSince() should invoke dao, pass argument and return result from dao"() {
@@ -440,7 +460,8 @@ class CategoryServiceImplTest extends Specification {
440460
when:
441461
service.getStatisticsOf(null, 'whatever')
442462
then:
443-
thrown IllegalArgumentException
463+
IllegalArgumentException ex = thrown()
464+
ex.message == 'Collection id must be non null'
444465
}
445466

446467
def "getStatisticsOf() should pass arguments to dao"() {
@@ -462,7 +483,8 @@ class CategoryServiceImplTest extends Specification {
462483
when:
463484
service.suggestCategoryForUser(null)
464485
then:
465-
thrown IllegalArgumentException
486+
IllegalArgumentException ex = thrown()
487+
ex.message == 'User id must be non null'
466488
}
467489

468490
def 'suggestCategoryForUser() should return category of the last created series'() {

src/test/groovy/ru/mystamps/web/feature/country/CountryServiceImplTest.groovy

+40-17
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class CountryServiceImplTest extends Specification {
5555
when:
5656
service.add(null, Random.userId())
5757
then:
58-
thrown IllegalArgumentException
58+
IllegalArgumentException ex = thrown()
59+
ex.message == 'DTO must be non null'
5960
}
6061

6162
def "add() should throw exception when country name in English is null"() {
@@ -64,14 +65,16 @@ class CountryServiceImplTest extends Specification {
6465
when:
6566
service.add(form, Random.userId())
6667
then:
67-
thrown IllegalArgumentException
68+
IllegalArgumentException ex = thrown()
69+
ex.message == 'Country name in English must be non null'
6870
}
6971

7072
def "add() should throw exception when user is null"() {
7173
when:
7274
service.add(form, null)
7375
then:
74-
thrown IllegalArgumentException
76+
IllegalArgumentException ex = thrown()
77+
ex.message == 'User id must be non null'
7578
}
7679

7780
def "add() should call dao"() {
@@ -91,11 +94,12 @@ class CountryServiceImplTest extends Specification {
9194

9295
def "add() should throw exception when name can't be converted to slug"() {
9396
given:
94-
form.setName(null)
97+
form.setName('-')
9598
when:
9699
service.add(form, Random.userId())
97100
then:
98-
thrown IllegalArgumentException
101+
IllegalArgumentException ex = thrown()
102+
ex.message == "Slug for string '-' must be non empty"
99103
}
100104

101105
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -169,7 +173,8 @@ class CountryServiceImplTest extends Specification {
169173
when:
170174
service.findIdsWhenNameStartsWith(nullOrBlank())
171175
then:
172-
thrown IllegalArgumentException
176+
IllegalArgumentException ex = thrown()
177+
ex.message == 'Name must be non-blank'
173178
}
174179

175180
def 'findIdsWhenNameStartsWith() should throw exception when name contains percent or underscore character'() {
@@ -178,7 +183,8 @@ class CountryServiceImplTest extends Specification {
178183
when:
179184
service.findIdsWhenNameStartsWith(invalidName)
180185
then:
181-
thrown IllegalArgumentException
186+
IllegalArgumentException ex = thrown()
187+
ex.message == "Name must not contain '%' or '_' chars"
182188
}
183189

184190
def 'findIdsWhenNameStartsWith() should invoke dao, pass argument and return result from dao'() {
@@ -229,17 +235,26 @@ class CountryServiceImplTest extends Specification {
229235
//
230236
// Tests for findOneAsLinkEntity()
231237
//
238+
239+
def 'findOneAsLinkEntity() should throw exception when country slug is null'() {
240+
when:
241+
service.findOneAsLinkEntity(null, Random.lang())
242+
then:
243+
IllegalArgumentException ex = thrown()
244+
ex.message == 'Country slug must be non null'
245+
}
246+
232247
@Unroll
233248
def "findOneAsLinkEntity() should throw exception when country slug is '#slug'"(String slug) {
234249
when:
235250
service.findOneAsLinkEntity(slug, 'ru')
236251
then:
237-
thrown IllegalArgumentException
252+
IllegalArgumentException ex = thrown()
253+
ex.message == 'Country slug must be non empty'
238254
where:
239255
slug | _
240256
' ' | _
241257
'' | _
242-
null | _
243258
}
244259

245260
def "findOneAsLinkEntity() should pass arguments to dao"() {
@@ -280,7 +295,8 @@ class CountryServiceImplTest extends Specification {
280295
when:
281296
service.countCountriesOf(null)
282297
then:
283-
thrown IllegalArgumentException
298+
IllegalArgumentException ex = thrown()
299+
ex.message == 'Collection id must be non null'
284300
}
285301

286302
def "countCountriesOf() should pass arguments to dao"() {
@@ -300,7 +316,8 @@ class CountryServiceImplTest extends Specification {
300316
when:
301317
service.countBySlug(null)
302318
then:
303-
thrown IllegalArgumentException
319+
IllegalArgumentException ex = thrown()
320+
ex.message == 'Country slug must be non null'
304321
}
305322

306323
def "countBySlug() should call dao"() {
@@ -320,7 +337,8 @@ class CountryServiceImplTest extends Specification {
320337
when:
321338
service.countByName(null)
322339
then:
323-
thrown IllegalArgumentException
340+
IllegalArgumentException ex = thrown()
341+
ex.message == 'Name must be non null'
324342
}
325343

326344
def "countByName() should call dao"() {
@@ -347,7 +365,8 @@ class CountryServiceImplTest extends Specification {
347365
when:
348366
service.countByNameRu(null)
349367
then:
350-
thrown IllegalArgumentException
368+
IllegalArgumentException ex = thrown()
369+
ex.message == 'Name in Russian must be non null'
351370
}
352371

353372
def "countByNameRu() should call dao"() {
@@ -374,7 +393,8 @@ class CountryServiceImplTest extends Specification {
374393
when:
375394
service.countAddedSince(null)
376395
then:
377-
thrown IllegalArgumentException
396+
IllegalArgumentException ex = thrown()
397+
ex.message == 'Date must be non null'
378398
}
379399

380400
def "countAddedSince() should invoke dao, pass argument and return result from dao"() {
@@ -398,7 +418,8 @@ class CountryServiceImplTest extends Specification {
398418
when:
399419
service.countUntranslatedNamesSince(null)
400420
then:
401-
thrown IllegalArgumentException
421+
IllegalArgumentException ex = thrown()
422+
ex.message == 'Date must be non null'
402423
}
403424

404425
def "countUntranslatedNamesSince() should invoke dao, pass argument and return result from dao"() {
@@ -422,7 +443,8 @@ class CountryServiceImplTest extends Specification {
422443
when:
423444
service.getStatisticsOf(null, 'whatever')
424445
then:
425-
thrown IllegalArgumentException
446+
IllegalArgumentException ex = thrown()
447+
ex.message == 'Collection id must be non null'
426448
}
427449

428450
def "getStatisticsOf() should pass arguments to dao"() {
@@ -444,7 +466,8 @@ class CountryServiceImplTest extends Specification {
444466
when:
445467
service.suggestCountryForUser(null)
446468
then:
447-
thrown IllegalArgumentException
469+
IllegalArgumentException ex = thrown()
470+
ex.message == 'User id must be non null'
448471
}
449472

450473
def 'suggestCountryForUser() should return country of the last created series'() {

0 commit comments

Comments
 (0)