Skip to content

Commit 7ccfdc5

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 8a6185c commit 7ccfdc5

10 files changed

+78
-35
lines changed

src/test/groovy/ru/mystamps/web/feature/collection/CollectionServiceImplTest.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ class CollectionServiceImplTest extends Specification {
324324
when:
325325
service.countSeriesOf(null)
326326
then:
327-
thrown IllegalArgumentException
327+
IllegalArgumentException ex = thrown()
328+
ex.message == 'Collection id must be non null'
328329
}
329330

330331
def 'countSeriesOf() should pass argument to dao'() {

src/test/groovy/ru/mystamps/web/feature/participant/ParticipantServiceImplTest.groovy

+8-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class ParticipantServiceImplTest extends Specification {
4646
when:
4747
service.add(null)
4848
then:
49-
thrown IllegalArgumentException
49+
IllegalArgumentException ex = thrown()
50+
ex.message == 'DTO must be non null'
5051
}
5152

5253
def 'add() should throw exception if name is null'() {
@@ -58,7 +59,8 @@ class ParticipantServiceImplTest extends Specification {
5859
when:
5960
service.add(form)
6061
then:
61-
thrown IllegalArgumentException
62+
IllegalArgumentException ex = thrown()
63+
ex.message == 'Name must be non null'
6264
}
6365

6466
def 'add() should throw exception when buyer flag is null'() {
@@ -70,7 +72,8 @@ class ParticipantServiceImplTest extends Specification {
7072
when:
7173
service.add(form)
7274
then:
73-
thrown IllegalArgumentException
75+
IllegalArgumentException ex = thrown()
76+
ex.message == 'Buyer flag must be non null'
7477
}
7578

7679
def 'add() should throw exception when seller flag is null'() {
@@ -82,7 +85,8 @@ class ParticipantServiceImplTest extends Specification {
8285
when:
8386
service.add(form)
8487
then:
85-
thrown IllegalArgumentException
88+
IllegalArgumentException ex = thrown()
89+
ex.message == 'Seller flag must be non null'
8690
}
8791

8892
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])

src/test/groovy/ru/mystamps/web/feature/series/sale/SeriesSalesServiceImplTest.groovy

+12-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class SeriesSalesServiceImplTest extends Specification {
5151
when:
5252
service.add(null, 123, 456)
5353
then:
54-
thrown IllegalArgumentException
54+
IllegalArgumentException ex = thrown()
55+
ex.message == 'DTO must be non null'
5556
}
5657

5758
def 'add() should throw exception when seller id is null'() {
@@ -60,7 +61,8 @@ class SeriesSalesServiceImplTest extends Specification {
6061
when:
6162
service.add(form, 123, 456)
6263
then:
63-
thrown IllegalArgumentException
64+
IllegalArgumentException ex = thrown()
65+
ex.message == 'Seller id must be non null'
6466
}
6567

6668
def 'add() should throw exception when price is null'() {
@@ -69,7 +71,8 @@ class SeriesSalesServiceImplTest extends Specification {
6971
when:
7072
service.add(form, 123, 456)
7173
then:
72-
thrown IllegalArgumentException
74+
IllegalArgumentException ex = thrown()
75+
ex.message == 'Price must be non null'
7376
}
7477

7578
def 'add() should throw exception when currency is null'() {
@@ -78,21 +81,24 @@ class SeriesSalesServiceImplTest extends Specification {
7881
when:
7982
service.add(form, 123, 456)
8083
then:
81-
thrown IllegalArgumentException
84+
IllegalArgumentException ex = thrown()
85+
ex.message == 'Currency must be non null'
8286
}
8387

8488
def 'add() should throw exception when series id is null'() {
8589
when:
8690
service.add(form, null, 456)
8791
then:
88-
thrown IllegalArgumentException
92+
IllegalArgumentException ex = thrown()
93+
ex.message == 'Series id must be non null'
8994
}
9095

9196
def 'add() should throw exception when user id is null'() {
9297
when:
9398
service.add(form, 123, null)
9499
then:
95-
thrown IllegalArgumentException
100+
IllegalArgumentException ex = thrown()
101+
ex.message == 'Current user id must be non null'
96102
}
97103

98104
@Unroll

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ class CronServiceImplTest extends Specification {
215215
when:
216216
service.purgeUsersActivations()
217217
then:
218-
thrown IllegalStateException
218+
IllegalStateException ex = thrown()
219+
ex.message == 'Expired activations must be non null'
219220
}
220221

221222
def "purgeUsersActivations() should delete expired activations"() {

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ class SiteServiceImplTest extends Specification {
120120
when:
121121
serviceImpl.logEvent(null, TEST_PAGE, TEST_METHOD, null, null, null, null, null)
122122
then:
123-
thrown IllegalArgumentException
123+
IllegalArgumentException ex = thrown()
124+
ex.message == 'Type of suspicious activity must be non null'
124125
}
125126

126127
def "logEvent() should throw exception when page is null"() {
127128
when:
128129
serviceImpl.logEvent(TEST_TYPE, null, TEST_METHOD, null, null, null, null, null)
129130
then:
130-
thrown IllegalArgumentException
131+
IllegalArgumentException ex = thrown()
132+
ex.message == 'Page must be non null'
131133
}
132134

133135
def "logEvent() should call dao"() {

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

+30-11
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
*/
1818
package ru.mystamps.web.service
1919

20-
import static io.qala.datagen.RandomShortApi.nullOrBlank
21-
2220
import org.slf4j.helpers.NOPLogger
2321

2422
import spock.lang.Specification
23+
import spock.lang.Unroll
2524

2625
import ru.mystamps.web.dao.StampsCatalogDao
2726
import ru.mystamps.web.tests.Random
@@ -45,14 +44,16 @@ class StampsCatalogServiceImplTest extends Specification {
4544
when:
4645
service.add(null)
4746
then:
48-
thrown IllegalArgumentException
47+
IllegalArgumentException ex = thrown()
48+
ex.message == 'TestCatalog numbers must be non null'
4949
}
5050

5151
def "add() should throw exception when numbers is empty"() {
5252
when:
5353
service.add([] as Set)
5454
then:
55-
thrown IllegalArgumentException
55+
IllegalArgumentException ex = thrown()
56+
ex.message == 'TestCatalog numbers must be non empty'
5657
}
5758

5859
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -76,21 +77,24 @@ class StampsCatalogServiceImplTest extends Specification {
7677
when:
7778
service.addToSeries(null, [ '1', '2' ] as Set)
7879
then:
79-
thrown IllegalArgumentException
80+
IllegalArgumentException ex = thrown()
81+
ex.message == 'Series id must be non null'
8082
}
8183

8284
def "addToSeries() should throw exception when numbers is null"() {
8385
when:
8486
service.addToSeries(123, null)
8587
then:
86-
thrown IllegalArgumentException
88+
IllegalArgumentException ex = thrown()
89+
ex.message == 'TestCatalog numbers must be non null'
8790
}
8891

8992
def "addToSeries() should throw exception when numbers is empty"() {
9093
when:
9194
service.addToSeries(123, [] as Set)
9295
then:
93-
thrown IllegalArgumentException
96+
IllegalArgumentException ex = thrown()
97+
ex.message == 'TestCatalog numbers must be non empty'
9498
}
9599

96100
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -118,7 +122,8 @@ class StampsCatalogServiceImplTest extends Specification {
118122
when:
119123
service.findBySeriesId(null)
120124
then:
121-
thrown IllegalArgumentException
125+
IllegalArgumentException ex = thrown()
126+
ex.message == 'Series id must be non null'
122127
}
123128

124129
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -142,11 +147,25 @@ class StampsCatalogServiceImplTest extends Specification {
142147
// Tests for findSeriesIdsByNumber()
143148
//
144149

145-
def 'findSeriesIdsByNumber() should throw exception when argument is null, empty or blank'() {
150+
def 'findSeriesIdsByNumber() should throw exception when argument is null'() {
151+
when:
152+
service.findSeriesIdsByNumber(null)
153+
then:
154+
IllegalArgumentException ex = thrown()
155+
ex.message == 'TestCatalog number must be non null'
156+
}
157+
158+
@Unroll
159+
def "findSeriesIdsByNumber() should throw exception when argument is '#catalogNumber'"(String catalogNumber) {
146160
when:
147-
service.findSeriesIdsByNumber(nullOrBlank())
161+
service.findSeriesIdsByNumber(catalogNumber)
148162
then:
149-
thrown IllegalArgumentException
163+
IllegalArgumentException ex = thrown()
164+
ex.message == 'TestCatalog number must be non-blank'
165+
where:
166+
catalogNumber | _
167+
'' | _
168+
' ' | _
150169
}
151170

152171
def 'findSeriesIdsByNumber() should invoke dao and return its result'() {

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class SuspiciousActivityServiceImplTest extends Specification {
5454
when:
5555
service.countByTypeSince(type, new Date())
5656
then:
57-
thrown IllegalArgumentException
57+
IllegalArgumentException ex = thrown()
58+
ex.message == 'Type must be non-blank'
5859
where:
5960
type | _
6061
null | _
@@ -66,7 +67,8 @@ class SuspiciousActivityServiceImplTest extends Specification {
6667
when:
6768
service.countByTypeSince('AnyType', null)
6869
then:
69-
thrown IllegalArgumentException
70+
IllegalArgumentException ex = thrown()
71+
ex.message == 'Date must be non null'
7072
}
7173

7274
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -98,7 +100,8 @@ class SuspiciousActivityServiceImplTest extends Specification {
98100
when:
99101
service.findSuspiciousActivities(page, 10)
100102
then:
101-
thrown IllegalArgumentException
103+
IllegalArgumentException ex = thrown()
104+
ex.message == 'Page must be greater than zero'
102105
where:
103106
page | _
104107
-1 | _
@@ -110,7 +113,8 @@ class SuspiciousActivityServiceImplTest extends Specification {
110113
when:
111114
service.findSuspiciousActivities(1, recordsPerPage)
112115
then:
113-
thrown IllegalArgumentException
116+
IllegalArgumentException ex = thrown()
117+
ex.message == 'RecordsPerPage must be greater than zero'
114118
where:
115119
recordsPerPage | _
116120
-1 | _

src/test/groovy/ru/mystamps/web/util/CatalogUtilsTest.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class CatalogUtilsTest extends Specification {
3131
when:
3232
CatalogUtils.toShortForm(null as List)
3333
then:
34-
thrown IllegalArgumentException
34+
IllegalArgumentException ex = thrown()
35+
ex.message == 'Catalog numbers must be non null'
3536
}
3637

3738
def 'toShortForm() should return empty string for empty numbers'() {

src/test/groovy/ru/mystamps/web/util/PagerTest.groovy

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,25 @@ class PagerTest extends Specification {
3131
when:
3232
new Pager(Long.MAX_VALUE, 10, 1)
3333
then:
34-
thrown ArithmeticException
34+
ArithmeticException ex = thrown()
35+
ex.message == 'integer overflow'
3536
}
3637

3738
def "Pager() should throw exception when total records is less than 0"() {
3839
when:
3940
new Pager(-1, 10, 1)
4041
then:
41-
thrown IllegalArgumentException
42+
IllegalArgumentException ex = thrown()
43+
ex.message == 'Total records must be greater than or equal to zero'
4244
}
4345

4446
@Unroll
4547
def "Pager() should throw exception when records per page (#recordsPerPage) is less than 1"(int recordsPerPage) {
4648
when:
4749
new Pager(10, recordsPerPage, 1)
4850
then:
49-
thrown IllegalArgumentException
51+
IllegalArgumentException ex = thrown()
52+
ex.message == 'Records per page must be greater than zero'
5053
where:
5154
recordsPerPage | _
5255
-1 | _
@@ -58,7 +61,8 @@ class PagerTest extends Specification {
5861
when:
5962
new Pager(10, 10, currentPage)
6063
then:
61-
thrown IllegalArgumentException
64+
IllegalArgumentException ex = thrown()
65+
ex.message == 'Current page must be greater than zero'
6266
where:
6367
currentPage | _
6468
-1 | _

src/test/groovy/ru/mystamps/web/util/SlugUtilsTest.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class SlugUtilsTest extends Specification {
3131
when:
3232
SlugUtils.slugify(null)
3333
then:
34-
thrown IllegalArgumentException
34+
IllegalArgumentException ex = thrown()
35+
ex.message == 'Text must be non null'
3536
}
3637

3738
@Unroll

0 commit comments

Comments
 (0)