Skip to content

Commit 33cbdd5

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 66752e8 commit 33cbdd5

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

src/test/groovy/ru/mystamps/web/feature/series/SeriesServiceImplTest.groovy

+42-22
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class SeriesServiceImplTest extends Specification {
8787
when:
8888
service.add(null, Random.userId(), bool())
8989
then:
90-
thrown IllegalArgumentException
90+
IllegalArgumentException ex = thrown()
91+
ex.message == 'DTO must be non null'
9192
}
9293

9394
def "add() should throw exception if quantity is null"() {
@@ -96,7 +97,8 @@ class SeriesServiceImplTest extends Specification {
9697
when:
9798
service.add(form, Random.userId(), bool())
9899
then:
99-
thrown IllegalArgumentException
100+
IllegalArgumentException ex = thrown()
101+
ex.message == 'Quantity must be non null'
100102
}
101103

102104
def "add() should throw exception if perforated is null"() {
@@ -105,7 +107,8 @@ class SeriesServiceImplTest extends Specification {
105107
when:
106108
service.add(form, Random.userId(), bool())
107109
then:
108-
thrown IllegalArgumentException
110+
IllegalArgumentException ex = thrown()
111+
ex.message == 'Perforated property must be non null'
109112
}
110113

111114
def "add() should throw exception if category is null"() {
@@ -114,7 +117,8 @@ class SeriesServiceImplTest extends Specification {
114117
when:
115118
service.add(form, Random.userId(), bool())
116119
then:
117-
thrown IllegalArgumentException
120+
IllegalArgumentException ex = thrown()
121+
ex.message == 'Category must be non null'
118122
}
119123

120124
def "add() should throw exception if category id is null"() {
@@ -123,14 +127,16 @@ class SeriesServiceImplTest extends Specification {
123127
when:
124128
service.add(form, Random.userId(), bool())
125129
then:
126-
thrown IllegalArgumentException
130+
IllegalArgumentException ex = thrown()
131+
ex.message == 'Category id must be non null'
127132
}
128133

129134
def "add() should throw exception when user is null"() {
130135
when:
131136
service.add(form, null, bool())
132137
then:
133-
thrown IllegalArgumentException
138+
IllegalArgumentException ex = thrown()
139+
ex.message == 'User id must be non null'
134140
}
135141

136142
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -257,7 +263,8 @@ class SeriesServiceImplTest extends Specification {
257263
when:
258264
service.add(form, Random.userId(), true)
259265
then:
260-
thrown IllegalArgumentException
266+
IllegalArgumentException ex = thrown()
267+
ex.message == 'Comment must be non empty'
261268
}
262269

263270
@Unroll
@@ -445,15 +452,16 @@ class SeriesServiceImplTest extends Specification {
445452
and:
446453
seriesDao.add(_ as AddSeriesDbDto) >> Random.id()
447454
and:
448-
imageService.addToSeries(_ as Integer, _ as Integer) >> { throw new IllegalStateException() }
455+
imageService.addToSeries(_ as Integer, _ as Integer) >> { throw new IllegalStateException('oops') }
449456
when:
450457
service.add(form, Random.userId(), bool())
451458
then:
452459
imageService.save(_) >> expectedImageInfo
453460
and:
454461
1 * imageService.removeIfPossible(expectedImageInfo)
455462
and:
456-
thrown IllegalStateException
463+
IllegalStateException ex = thrown()
464+
ex.message == 'oops'
457465
}
458466

459467
//
@@ -464,21 +472,24 @@ class SeriesServiceImplTest extends Specification {
464472
when:
465473
service.addImageToSeries(null, Random.id(), Random.userId())
466474
then:
467-
thrown IllegalArgumentException
475+
IllegalArgumentException ex = thrown()
476+
ex.message == 'DTO must be non null'
468477
}
469478

470479
def "addImageToSeries() should throw exception when series id is null"() {
471480
when:
472481
service.addImageToSeries(imageForm, null, Random.userId())
473482
then:
474-
thrown IllegalArgumentException
483+
IllegalArgumentException ex = thrown()
484+
ex.message == 'Series id must be non null'
475485
}
476486

477487
def "addImageToSeries() should throw exception when user id is null"() {
478488
when:
479489
service.addImageToSeries(imageForm, Random.id(), null)
480490
then:
481-
thrown IllegalArgumentException
491+
IllegalArgumentException ex = thrown()
492+
ex.message == 'User id must be non null'
482493
}
483494

484495
@SuppressWarnings('UnnecessaryReturnKeyword')
@@ -525,15 +536,16 @@ class SeriesServiceImplTest extends Specification {
525536
given:
526537
ImageInfoDto expectedImageInfo = TestObjects.createImageInfoDto()
527538
and:
528-
imageService.addToSeries(_ as Integer, _ as Integer) >> { throw new IllegalStateException() }
539+
imageService.addToSeries(_ as Integer, _ as Integer) >> { throw new IllegalStateException('oops') }
529540
when:
530541
service.addImageToSeries(imageForm, Random.id(), Random.userId())
531542
then:
532543
imageService.save(_) >> expectedImageInfo
533544
and:
534545
1 * imageService.removeIfPossible(expectedImageInfo)
535546
and:
536-
thrown IllegalStateException
547+
IllegalStateException ex = thrown()
548+
ex.message == 'oops'
537549
}
538550

539551
//
@@ -574,7 +586,8 @@ class SeriesServiceImplTest extends Specification {
574586
when:
575587
service.countAddedSince(null)
576588
then:
577-
thrown IllegalArgumentException
589+
IllegalArgumentException ex = thrown()
590+
ex.message == 'Date must be non null'
578591
}
579592

580593
def "countAddedSince() should invoke dao, pass argument and return result from dao"() {
@@ -598,7 +611,8 @@ class SeriesServiceImplTest extends Specification {
598611
when:
599612
service.countUpdatedSince(null)
600613
then:
601-
thrown IllegalArgumentException
614+
IllegalArgumentException ex = thrown()
615+
ex.message == 'Date must be non null'
602616
}
603617

604618
def "countUpdatedSince() should invoke dao, pass argument and return result from dao"() {
@@ -622,7 +636,8 @@ class SeriesServiceImplTest extends Specification {
622636
when:
623637
service.isSeriesExist(null)
624638
then:
625-
thrown IllegalArgumentException
639+
IllegalArgumentException ex = thrown()
640+
ex.message == 'Series id must be non null'
626641
}
627642

628643
@Unroll
@@ -650,7 +665,8 @@ class SeriesServiceImplTest extends Specification {
650665
when:
651666
service.findQuantityById(null)
652667
then:
653-
thrown IllegalArgumentException
668+
IllegalArgumentException ex = thrown()
669+
ex.message == 'Series id must be non null'
654670
}
655671

656672
def 'findQuantityById() should invoke dao, pass argument and return result from dao'() {
@@ -1009,7 +1025,8 @@ class SeriesServiceImplTest extends Specification {
10091025
when:
10101026
service.findByCategorySlug(null, Random.lang())
10111027
then:
1012-
thrown IllegalArgumentException
1028+
IllegalArgumentException ex = thrown()
1029+
ex.message == 'Category slug must be non null'
10131030
}
10141031

10151032
def "findByCategorySlug() should call dao and return result"() {
@@ -1033,7 +1050,8 @@ class SeriesServiceImplTest extends Specification {
10331050
when:
10341051
service.findByCountrySlug(null, Random.lang())
10351052
then:
1036-
thrown IllegalArgumentException
1053+
IllegalArgumentException ex = thrown()
1054+
ex.message == 'Country slug must be non null'
10371055
}
10381056

10391057
def "findByCountrySlug() should call dao and return result"() {
@@ -1058,7 +1076,8 @@ class SeriesServiceImplTest extends Specification {
10581076
when:
10591077
service.findRecentlyAdded(quantity, null)
10601078
then:
1061-
thrown IllegalArgumentException
1079+
IllegalArgumentException ex = thrown()
1080+
ex.message == 'Quantity of recently added series must be greater than 0'
10621081
where:
10631082
quantity | _
10641083
-1 | _
@@ -1100,7 +1119,8 @@ class SeriesServiceImplTest extends Specification {
11001119
when:
11011120
service.findPurchasesAndSales(null)
11021121
then:
1103-
thrown IllegalArgumentException
1122+
IllegalArgumentException ex = thrown()
1123+
ex.message == 'Series id must be non null'
11041124
}
11051125

11061126
def "findPurchasesAndSales() should invoke dao, pass argument and return result from dao"() {

0 commit comments

Comments
 (0)