Skip to content

Commit f061cbb

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 7ccfdc5 commit f061cbb

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

src/test/groovy/ru/mystamps/web/feature/account/UserServiceImplTest.groovy

+16-8
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class UserServiceImplTest extends Specification {
7070
when:
7171
service.registerUser(null)
7272
then:
73-
thrown IllegalArgumentException
73+
IllegalArgumentException ex = thrown()
74+
ex.message == 'DTO must be non null'
7475
}
7576

7677
def "registerUser() should create user"() {
@@ -101,7 +102,8 @@ class UserServiceImplTest extends Specification {
101102
when:
102103
service.registerUser(activationForm)
103104
then:
104-
thrown IllegalArgumentException
105+
IllegalArgumentException ex = thrown()
106+
ex.message == 'Activation key must be non null'
105107
}
106108

107109
def "registerUser() should do nothing when registration request not found"() {
@@ -204,7 +206,8 @@ class UserServiceImplTest extends Specification {
204206
when:
205207
service.registerUser(activationForm)
206208
then:
207-
thrown IllegalArgumentException
209+
IllegalArgumentException ex = thrown()
210+
ex.message == 'Password must be non null'
208211
}
209212

210213
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -231,7 +234,8 @@ class UserServiceImplTest extends Specification {
231234
then:
232235
encoder.encode(_ as String) >> null
233236
and:
234-
thrown IllegalStateException
237+
IllegalStateException ex = thrown()
238+
ex.message == 'Generated hash must be non null'
235239
}
236240

237241
def "registerUser() should throw exception when login is null"() {
@@ -240,7 +244,8 @@ class UserServiceImplTest extends Specification {
240244
when:
241245
service.registerUser(activationForm)
242246
then:
243-
thrown IllegalArgumentException
247+
IllegalArgumentException ex = thrown()
248+
ex.message == 'Login must be non null'
244249
}
245250

246251
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -294,7 +299,8 @@ class UserServiceImplTest extends Specification {
294299
when:
295300
service.findUserDetailsByLogin(null)
296301
then:
297-
thrown IllegalArgumentException
302+
IllegalArgumentException ex = thrown()
303+
ex.message == 'Login must be non null'
298304
}
299305

300306
def "findUserDetailsByLogin() should call dao"() {
@@ -322,7 +328,8 @@ class UserServiceImplTest extends Specification {
322328
when:
323329
service.countByLogin(null)
324330
then:
325-
thrown IllegalArgumentException
331+
IllegalArgumentException ex = thrown()
332+
ex.message == 'Login must be non null'
326333
}
327334

328335
def "countByLogin() should call dao"() {
@@ -349,7 +356,8 @@ class UserServiceImplTest extends Specification {
349356
when:
350357
service.countRegisteredSince(null)
351358
then:
352-
thrown IllegalArgumentException
359+
IllegalArgumentException ex = thrown()
360+
ex.message == 'Date must be non null'
353361
}
354362

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

src/test/groovy/ru/mystamps/web/feature/account/UsersActivationServiceImplTest.groovy

+14-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class UsersActivationServiceImplTest extends Specification {
5454
when:
5555
service.add(null, ANY_LOCALE)
5656
then:
57-
thrown IllegalArgumentException
57+
IllegalArgumentException ex = thrown()
58+
ex.message == 'DTO must be non null'
5859
}
5960

6061
def "add() should call dao"() {
@@ -106,7 +107,8 @@ class UsersActivationServiceImplTest extends Specification {
106107
when:
107108
service.add(registrationForm, ANY_LOCALE)
108109
then:
109-
thrown IllegalArgumentException
110+
IllegalArgumentException ex = thrown()
111+
ex.message == 'Email must be non null'
110112
}
111113

112114
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -176,7 +178,8 @@ class UsersActivationServiceImplTest extends Specification {
176178
when:
177179
service.countByActivationKey(null)
178180
then:
179-
thrown IllegalArgumentException
181+
IllegalArgumentException ex = thrown()
182+
ex.message == 'Activation key must be non null'
180183
}
181184

182185
def "countByActivationKey() should call dao"() {
@@ -203,7 +206,8 @@ class UsersActivationServiceImplTest extends Specification {
203206
when:
204207
service.countCreatedSince(null)
205208
then:
206-
thrown IllegalArgumentException
209+
IllegalArgumentException ex = thrown()
210+
ex.message == 'Date must be non null'
207211
}
208212

209213
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -231,7 +235,8 @@ class UsersActivationServiceImplTest extends Specification {
231235
when:
232236
service.findByActivationKey(null)
233237
then:
234-
thrown IllegalArgumentException
238+
IllegalArgumentException ex = thrown()
239+
ex.message == 'Activation key must be non null'
235240
}
236241

237242
def "findByActivationKey() should call dao, pass argument to it and return result"() {
@@ -253,7 +258,8 @@ class UsersActivationServiceImplTest extends Specification {
253258
when:
254259
service.findOlderThan(-1)
255260
then:
256-
thrown IllegalArgumentException
261+
IllegalArgumentException ex = thrown()
262+
ex.message == 'Days must be greater than zero'
257263
}
258264

259265
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -283,7 +289,8 @@ class UsersActivationServiceImplTest extends Specification {
283289
when:
284290
service.remove(null)
285291
then:
286-
thrown IllegalArgumentException
292+
IllegalArgumentException ex = thrown()
293+
ex.message == 'Activation key must be non null'
287294
}
288295

289296
def "remove() should pass argument to DAO method"() {

src/test/groovy/ru/mystamps/web/feature/image/ImageServiceImplTest.groovy

+38-13
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class ImageServiceImplTest extends Specification {
5858
when:
5959
service.save(null)
6060
then:
61-
thrown IllegalArgumentException
61+
IllegalArgumentException ex = thrown()
62+
ex.message == 'File must be non null'
6263
}
6364

6465
def "save() should throw exception if file has zero size"() {
@@ -67,7 +68,8 @@ class ImageServiceImplTest extends Specification {
6768
then:
6869
multipartFile.size >> 0L
6970
and:
70-
thrown IllegalArgumentException
71+
IllegalArgumentException ex = thrown()
72+
ex.message == 'Image size must be greater than zero'
7173
}
7274

7375
def "save() should throw exception if content type is null"() {
@@ -76,7 +78,8 @@ class ImageServiceImplTest extends Specification {
7678
then:
7779
multipartFile.contentType >> null
7880
and:
79-
thrown IllegalArgumentException
81+
IllegalArgumentException ex = thrown()
82+
ex.message == 'File type must be non null'
8083
}
8184

8285
def "save() should throw exception for unsupported content type"() {
@@ -85,7 +88,8 @@ class ImageServiceImplTest extends Specification {
8588
then:
8689
multipartFile.contentType >> 'image/tiff'
8790
and:
88-
thrown IllegalStateException
91+
IllegalStateException ex = thrown()
92+
ex.message == "File type must be PNG or JPEG image, but 'image/tiff' (tiff) were passed"
8993
}
9094

9195
@Unroll
@@ -168,7 +172,8 @@ class ImageServiceImplTest extends Specification {
168172
and:
169173
0 * imagePersistenceStrategy.save(_ as MultipartFile, _ as ImageInfoDto)
170174
and:
171-
thrown ImagePersistenceException
175+
ImagePersistenceException ex = thrown()
176+
ex.message == "Can't save image"
172177
}
173178

174179
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -207,15 +212,23 @@ class ImageServiceImplTest extends Specification {
207212
// Tests for get()
208213
//
209214

215+
def 'get() should throw exception if image id is null'() {
216+
when:
217+
service.get(null)
218+
then:
219+
IllegalArgumentException ex = thrown()
220+
ex.message == 'Image id must be non null'
221+
}
222+
210223
@Unroll
211224
def "get() should throw exception if image id is #imageId"(Integer imageId) {
212225
when:
213226
service.get(imageId)
214227
then:
215-
thrown IllegalArgumentException
228+
IllegalArgumentException ex = thrown()
229+
ex.message == 'Image id must be greater than zero'
216230
where:
217231
imageId | _
218-
null | _
219232
-1 | _
220233
0 | _
221234
}
@@ -277,15 +290,23 @@ class ImageServiceImplTest extends Specification {
277290
// Tests for getOrCreatePreview()
278291
//
279292

293+
def 'getOrCreatePreview() should throw exception if image id is null'() {
294+
when:
295+
service.getOrCreatePreview(null)
296+
then:
297+
IllegalArgumentException ex = thrown()
298+
ex.message == 'Image id must be non null'
299+
}
300+
280301
@Unroll
281302
def "getOrCreatePreview() should throw exception if image id is #imageId"(Integer imageId) {
282303
when:
283304
service.getOrCreatePreview(imageId)
284305
then:
285-
thrown IllegalArgumentException
306+
IllegalArgumentException ex = thrown()
307+
ex.message == 'Image id must be greater than zero'
286308
where:
287309
imageId | _
288-
null | _
289310
-1 | _
290311
0 | _
291312
}
@@ -317,14 +338,16 @@ class ImageServiceImplTest extends Specification {
317338
when:
318339
service.addToSeries(null, 1)
319340
then:
320-
thrown IllegalArgumentException
341+
IllegalArgumentException ex = thrown()
342+
ex.message == 'Series id must be non null'
321343
}
322344

323345
def "addToSeries() should throw exception when image id is null"() {
324346
when:
325347
service.addToSeries(1, null)
326348
then:
327-
thrown IllegalArgumentException
349+
IllegalArgumentException ex = thrown()
350+
ex.message == 'Image id must be non null'
328351
}
329352

330353
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -352,7 +375,8 @@ class ImageServiceImplTest extends Specification {
352375
when:
353376
service.findBySeriesId(null)
354377
then:
355-
thrown IllegalArgumentException
378+
IllegalArgumentException ex = thrown()
379+
ex.message == 'Series id must be non null'
356380
}
357381

358382
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -380,7 +404,8 @@ class ImageServiceImplTest extends Specification {
380404
when:
381405
service.removeIfPossible(null)
382406
then:
383-
thrown IllegalArgumentException
407+
IllegalArgumentException ex = thrown()
408+
ex.message == 'Image info must be non null'
384409
}
385410

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

0 commit comments

Comments
 (0)