Skip to content

Commit ea26423

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

File tree

2 files changed

+187
-204
lines changed

2 files changed

+187
-204
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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 spock.lang.Specification
21+
22+
import ru.mystamps.web.dao.CountryDao
23+
import ru.mystamps.web.entity.Country
24+
import ru.mystamps.web.entity.User
25+
import ru.mystamps.web.model.AddCountryForm
26+
import ru.mystamps.web.tests.DateUtils
27+
28+
public class CountryServiceTest extends Specification {
29+
30+
private AddCountryForm form
31+
private User user
32+
33+
private CountryDao countryDao = Mock()
34+
private CountryService service = new CountryServiceImpl(countryDao)
35+
36+
def setup() {
37+
form = new AddCountryForm()
38+
form.setName("Any country name")
39+
40+
user = TestObjects.createUser()
41+
}
42+
43+
//
44+
// Tests for add()
45+
//
46+
47+
def "add() should throw exception when dto is null"() {
48+
when:
49+
service.add(null, user)
50+
then:
51+
thrown IllegalArgumentException
52+
}
53+
54+
def "add() should throw exception when country name is null"() {
55+
given:
56+
form.setName(null)
57+
when:
58+
service.add(form, user)
59+
then:
60+
thrown IllegalArgumentException
61+
}
62+
63+
def "add() should throw exception when user is null"() {
64+
when:
65+
service.add(form, null)
66+
then:
67+
thrown IllegalArgumentException
68+
}
69+
70+
def "add() should call dao"() {
71+
given:
72+
Country expected = TestObjects.createCountry()
73+
countryDao.save(_ as Country) >> expected
74+
when:
75+
Country actual = service.add(form, user)
76+
then:
77+
actual == expected
78+
}
79+
80+
def "add() should pass country name to dao"() {
81+
given:
82+
String expectedCountryName = "Italy"
83+
form.setName(expectedCountryName)
84+
when:
85+
service.add(form, user)
86+
then:
87+
1 * countryDao.save({ Country country ->
88+
assert country?.name == expectedCountryName
89+
return true
90+
})
91+
}
92+
93+
def "add() should assign created at to current date"() {
94+
when:
95+
service.add(form, user)
96+
then:
97+
1 * countryDao.save({ Country country ->
98+
assert DateUtils.roughlyEqual(country?.metaInfo?.createdAt, new Date())
99+
return true
100+
})
101+
}
102+
103+
def "add() should assign updated at to current date"() {
104+
when:
105+
service.add(form, user)
106+
then:
107+
1 * countryDao.save({ Country country ->
108+
assert DateUtils.roughlyEqual(country?.metaInfo?.updatedAt, new Date())
109+
return true
110+
})
111+
}
112+
113+
def "add() should assign created by to user"() {
114+
when:
115+
service.add(form, user)
116+
then:
117+
1 * countryDao.save({ Country country ->
118+
assert country?.metaInfo?.createdBy == user
119+
return true
120+
})
121+
}
122+
123+
def "add() should assign updated by to user"() {
124+
when:
125+
service.add(form, user)
126+
then:
127+
1 * countryDao.save({ Country country ->
128+
assert country?.metaInfo?.updatedBy == user
129+
return true
130+
})
131+
}
132+
133+
//
134+
// Tests for findAll()
135+
//
136+
137+
def "findAll() should call dao"() {
138+
given:
139+
Country country1 = TestObjects.createCountry()
140+
country1.setName("First Country")
141+
and:
142+
Country country2 = TestObjects.createCountry()
143+
country2.setName("Second Country")
144+
and:
145+
List<Country> expectedCountries = new ArrayList<Country>()
146+
expectedCountries.add(country1)
147+
expectedCountries.add(country2)
148+
and:
149+
countryDao.findAll() >> expectedCountries
150+
when:
151+
Iterable<Country> resultCountries = service.findAll()
152+
then:
153+
resultCountries == expectedCountries
154+
}
155+
156+
//
157+
// Tests for findByName()
158+
//
159+
160+
def "findByName() should throw exception when name is null"() {
161+
when:
162+
service.findByName(null)
163+
then:
164+
thrown IllegalArgumentException
165+
}
166+
167+
def "findByName() should call dao"() {
168+
given:
169+
Country expectedCountry = TestObjects.createCountry()
170+
countryDao.findByName(_ as String) >> expectedCountry
171+
when:
172+
Country country = service.findByName("Any name here")
173+
then:
174+
country == expectedCountry
175+
}
176+
177+
def "findByName() should pass country name to dao"() {
178+
when:
179+
service.findByName("Canada")
180+
then:
181+
1 * countryDao.findByName({ String name ->
182+
assert name == "Canada"
183+
return true
184+
})
185+
}
186+
187+
}

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

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

0 commit comments

Comments
 (0)