Skip to content

Commit aa785a3

Browse files
committed
ImageServiceTest: ported to Spock Framework.
Addressed to #151
1 parent c6dc3c8 commit aa785a3

File tree

3 files changed

+147
-157
lines changed

3 files changed

+147
-157
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<junit.version>4.10</junit.version>
4141
<mockito.version>1.9.5</mockito.version>
4242
<spock.version>0.7-groovy-2.0</spock.version>
43+
<cglib.version>2.2.2</cglib.version>
4344
<groovy.version>2.0.8</groovy.version>
4445
<fest.assert.version>2.0M8</fest.assert.version>
4546

@@ -274,6 +275,14 @@
274275
<scope>test</scope>
275276
</dependency>
276277

278+
<!-- For mocking of non-interface types -->
279+
<dependency>
280+
<groupId>cglib</groupId>
281+
<artifactId>cglib-nodep</artifactId>
282+
<version>${cglib.version}</version>
283+
<scope>test</scope>
284+
</dependency>
285+
277286
<dependency>
278287
<groupId>org.easytesting</groupId>
279288
<artifactId>fest-assert-core</artifactId>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (C) 2009-2013 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service
19+
20+
import org.springframework.web.multipart.MultipartFile
21+
22+
import spock.lang.Specification
23+
24+
import ru.mystamps.web.dao.ImageDao
25+
import ru.mystamps.web.entity.Image
26+
27+
public class ImageServiceTest extends Specification {
28+
29+
private ImageDao imageDao = Mock()
30+
private MultipartFile multipartFile = Mock()
31+
32+
private ImageService service = new ImageServiceImpl(imageDao)
33+
34+
def setup() {
35+
multipartFile.getSize() >> 1024L
36+
multipartFile.getContentType() >> "image/png"
37+
imageDao.save(_ as Image) >> new Image()
38+
}
39+
40+
//
41+
// Tests for save()
42+
//
43+
44+
def "save() should throw exception if file is null"() {
45+
when:
46+
service.save(null)
47+
then:
48+
thrown IllegalArgumentException
49+
}
50+
51+
def "save() should throw exception if file has zero size"() {
52+
when:
53+
service.save(multipartFile)
54+
then:
55+
multipartFile.getSize() >> 0L
56+
and:
57+
thrown IllegalArgumentException
58+
}
59+
60+
def "save() should throw exception if content type is null"() {
61+
when:
62+
service.save(multipartFile)
63+
then:
64+
multipartFile.getContentType() >> null
65+
and:
66+
thrown IllegalArgumentException
67+
}
68+
69+
def "save() should throw exception for unsupported content type"() {
70+
when:
71+
service.save(multipartFile)
72+
then:
73+
multipartFile.getContentType() >> "image/tiff"
74+
and:
75+
thrown IllegalStateException
76+
}
77+
78+
def "save() should pass image to image dao"() {
79+
when:
80+
service.save(multipartFile)
81+
then:
82+
1 * imageDao.save(_ as Image) >> new Image()
83+
}
84+
85+
def "save() should convert IOException to RuntimeException"() {
86+
given:
87+
multipartFile.getBytes() >> { throw new IOException() }
88+
when:
89+
service.save(multipartFile)
90+
then:
91+
RuntimeException ex = thrown()
92+
and:
93+
ex.cause instanceof IOException
94+
}
95+
96+
def "save() should pass file content to dao"() {
97+
given:
98+
byte[] expected = "test".getBytes()
99+
multipartFile.getBytes() >> expected
100+
when:
101+
service.save(multipartFile)
102+
then:
103+
1 * imageDao.save({ Image image ->
104+
assert image?.data == expected
105+
return true
106+
}) >> new Image()
107+
}
108+
109+
def "save() should pass content type to dao"() {
110+
when:
111+
service.save(multipartFile)
112+
then:
113+
multipartFile.getContentType() >> "image/jpeg"
114+
and:
115+
1 * imageDao.save({ Image image ->
116+
assert image?.type == Image.Type.JPEG
117+
return true
118+
}) >> new Image()
119+
}
120+
121+
def "save() should return url with image"() {
122+
given:
123+
Integer expectedImageId = 10
124+
and:
125+
Image image = Mock()
126+
when:
127+
String result = service.save(multipartFile)
128+
then:
129+
image.getId() >> expectedImageId
130+
and:
131+
imageDao.save(_ as Image) >> image
132+
and:
133+
String expectedUrl =
134+
ImageService.GET_IMAGE_PAGE.replace("{id}", String.valueOf(expectedImageId))
135+
result == expectedUrl
136+
}
137+
138+
}

src/test/java/ru/mystamps/web/service/ImageServiceTest.java

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)