Skip to content

Commit 1c9418f

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 7a05b0c commit 1c9418f

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/test/groovy/ru/mystamps/web/feature/series/importing/SeriesImportServiceImplTest.groovy

+40-17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import static io.qala.datagen.RandomShortApi.nullOrBlank
2323
import static io.qala.datagen.RandomValue.between
2424

2525
import spock.lang.Specification
26+
import spock.lang.Unroll
2627

2728
import org.slf4j.helpers.NOPLogger
2829

@@ -76,7 +77,8 @@ class SeriesImportServiceImplTest extends Specification {
7677
when:
7778
service.addRequest(null, Random.userId())
7879
then:
79-
thrown IllegalArgumentException
80+
IllegalArgumentException ex = thrown()
81+
ex.message == 'DTO must be non null'
8082
}
8183

8284
def 'addRequest() should throw exception if url is null'() {
@@ -85,14 +87,18 @@ class SeriesImportServiceImplTest extends Specification {
8587
when:
8688
service.addRequest(form, Random.userId())
8789
then:
88-
thrown IllegalArgumentException
90+
IllegalArgumentException ex = thrown()
91+
ex.message == 'URL must be non null'
8992
}
9093

9194
def 'addRequest() should throw exception if user id is null'() {
95+
given:
96+
form.setUrl(Random.url())
9297
when:
9398
service.addRequest(form, null)
9499
then:
95-
thrown IllegalArgumentException
100+
IllegalArgumentException ex = thrown()
101+
ex.message == 'User id must be non null'
96102
}
97103

98104
def 'addRequest() should throw exception if url is incorrect'() {
@@ -271,21 +277,24 @@ class SeriesImportServiceImplTest extends Specification {
271277
when:
272278
service.changeStatus(null, oldStatus, newStatus)
273279
then:
274-
thrown IllegalArgumentException
280+
IllegalArgumentException ex = thrown()
281+
ex.message == 'Request id must be non null'
275282
}
276283

277284
def 'changeStatus() should throw exception when old status is blank'() {
278285
when:
279286
service.changeStatus(Random.id(), nullOrBlank(), Random.importRequestStatus())
280287
then:
281-
thrown IllegalArgumentException
288+
IllegalArgumentException ex = thrown()
289+
ex.message == 'Old status must be non-blank'
282290
}
283291

284292
def 'changeStatus() should throw exception when new status is blank'() {
285293
when:
286294
service.changeStatus(Random.id(), Random.importRequestStatus(), nullOrBlank())
287295
then:
288-
thrown IllegalArgumentException
296+
IllegalArgumentException ex = thrown()
297+
ex.message == 'New status must be non-blank'
289298
}
290299

291300
def 'changeStatus() should throw exception when statuses are equal'() {
@@ -294,7 +303,8 @@ class SeriesImportServiceImplTest extends Specification {
294303
when:
295304
service.changeStatus(Random.id(), status, status)
296305
then:
297-
thrown IllegalArgumentException
306+
IllegalArgumentException ex = thrown()
307+
ex.message == 'Statuses must be different'
298308
}
299309

300310
@SuppressWarnings('UnnecessaryReturnKeyword')
@@ -327,7 +337,8 @@ class SeriesImportServiceImplTest extends Specification {
327337
when:
328338
service.findById(null)
329339
then:
330-
thrown IllegalArgumentException
340+
IllegalArgumentException ex = thrown()
341+
ex.message == 'Request id must be non null'
331342
}
332343

333344
def 'findById() should invoke dao, pass argument and return result from dao'() {
@@ -353,14 +364,21 @@ class SeriesImportServiceImplTest extends Specification {
353364
when:
354365
service.saveDownloadedContent(null, content)
355366
then:
356-
thrown IllegalArgumentException
367+
IllegalArgumentException ex = thrown()
368+
ex.message == 'Request id must be non null'
357369
}
358370

359-
def 'saveDownloadedContent() should throw exception when content is blank'() {
371+
@Unroll
372+
def "saveDownloadedContent() should throw exception when content is '#content'"(String content) {
360373
when:
361-
service.saveDownloadedContent(Random.id(), nullOrBlank())
374+
service.saveDownloadedContent(Random.id(), content)
362375
then:
363-
thrown IllegalArgumentException
376+
IllegalArgumentException ex = thrown()
377+
ex.message == 'Content must be non-blank'
378+
where:
379+
content | _
380+
'' | _
381+
' ' | _
364382
}
365383

366384
@SuppressWarnings('UnnecessaryReturnKeyword')
@@ -411,7 +429,8 @@ class SeriesImportServiceImplTest extends Specification {
411429
when:
412430
service.getDownloadedContent(null)
413431
then:
414-
thrown IllegalArgumentException
432+
IllegalArgumentException ex = thrown()
433+
ex.message == 'Request id must be non null'
415434
}
416435

417436
def 'getDownloadedContent() should invoke dao, pass argument and return result from dao'() {
@@ -437,14 +456,16 @@ class SeriesImportServiceImplTest extends Specification {
437456
when:
438457
service.saveParsedData(null, TestObjects.createEmptySeriesExtractedInfo(), Random.url())
439458
then:
440-
thrown IllegalArgumentException
459+
IllegalArgumentException ex = thrown()
460+
ex.message == 'Request id must be non null'
441461
}
442462

443463
def 'saveParsedData() should throw exception when series info is null'() {
444464
when:
445465
service.saveParsedData(Random.id(), null, Random.url())
446466
then:
447-
thrown IllegalArgumentException
467+
IllegalArgumentException ex = thrown()
468+
ex.message == 'Series info must be non null'
448469
}
449470

450471
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
@@ -566,7 +587,8 @@ class SeriesImportServiceImplTest extends Specification {
566587
when:
567588
service.getParsedData(null, Random.lang())
568589
then:
569-
thrown IllegalArgumentException
590+
IllegalArgumentException ex = thrown()
591+
ex.message == 'Request id must be non null'
570592
}
571593

572594
def 'getParsedData() should invoke dao, pass argument and return result from dao'() {
@@ -591,7 +613,8 @@ class SeriesImportServiceImplTest extends Specification {
591613
when:
592614
service.findRequestInfo(null)
593615
then:
594-
thrown IllegalArgumentException
616+
IllegalArgumentException ex = thrown()
617+
ex.message == 'Series id must be non null'
595618
}
596619

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

0 commit comments

Comments
 (0)