11package ir.bigz.springbootreal.web
22
3+ import ir.bigz.springbootreal.commons.util.Utils
4+ import ir.bigz.springbootreal.configuration.CacheConfiguration
5+ import ir.bigz.springbootreal.configuration.DataSourceConfiguration
6+ import ir.bigz.springbootreal.configuration.WebConfiguration
37import ir.bigz.springbootreal.controller.SampleController
8+ import ir.bigz.springbootreal.dal.UserRepository
49import ir.bigz.springbootreal.dao.User
10+ import ir.bigz.springbootreal.dao.mapper.UserMapper
11+ import ir.bigz.springbootreal.dao.mapper.UserMapperImpl
12+ import ir.bigz.springbootreal.exception.validation.ErrorController
13+ import ir.bigz.springbootreal.exception.validation.ValidationErrorResponseModel
514import ir.bigz.springbootreal.service.UserService
615import ir.bigz.springbootreal.service.UserServiceImpl
16+ import ir.bigz.springbootreal.validation.*
17+ import ir.bigz.springbootreal.validation.annotation.Validator
718import ir.bigz.springbootreal.viewmodel.UserModel
819import org.junit.jupiter.api.Test
920import org.spockframework.spring.SpringBean
1021import org.springframework.beans.factory.annotation.Autowired
11- import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
12- import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient
13- import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
14- import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc
15- import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
22+ import org.springframework.boot.autoconfigure.EnableAutoConfiguration
23+ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
24+ import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
25+ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
1626import org.springframework.boot.test.context.SpringBootTest
1727import org.springframework.boot.test.web.client.TestRestTemplate
1828import org.springframework.context.ApplicationContext
19- import org.springframework.http.HttpMethod
20- import org.springframework.http.HttpStatus
21- import org.springframework.http.ResponseEntity
22- import org.springframework.test.context.ActiveProfiles
29+ import org.springframework.core.ParameterizedTypeReference
30+ import org.springframework.http.*
2331import org.springframework.test.context.ContextConfiguration
24- import org.springframework.web.client.RestTemplate
32+ import org.springframework.transaction.annotation.EnableTransactionManagement
2533import spock.lang.Specification
2634import spock.lang.Subject
2735import spock.lang.Title
2836
29- @ContextConfiguration (classes = [SampleController . class, UserServiceImpl . class, UserModel . class, User . class])
30- @Title (" sample spock test for testing project" )
31- @ActiveProfiles (" test" )
32- @AutoConfigureWebClient
33- @AutoConfigureWebMvc
34- @WebMvcTest (excludeAutoConfiguration = [SecurityAutoConfiguration . class])
37+ @ContextConfiguration (classes = [SampleController . class, UserServiceImpl . class, UserModel . class, User . class,
38+ UserMapper . class, UserMapperImpl . class, UserRepository . class,DataSourceConfiguration . class,
39+ WebConfiguration . class, CacheConfiguration . class,
40+ ValidationHandler . class, ValidationUtilsImpl . class, ValidationValidator . class,
41+ ValidationErrorResponseModel . class,ErrorController . class, ValidationType . class])
42+ @Title (" sample controller mock test" )
43+ @SpringBootTest (properties = " spring.profiles.active:test" , webEnvironment = SpringBootTest.WebEnvironment .RANDOM_PORT )
44+ @EnableAutoConfiguration (exclude = [DataSourceAutoConfiguration . class,
45+ HibernateJpaAutoConfiguration . class,
46+ DataSourceTransactionManagerAutoConfiguration . class])
47+ @EnableTransactionManagement
3548class SampleApiTest extends Specification {
3649
3750 @Autowired
3851 ApplicationContext applicationContext
3952
53+ @Autowired
54+ private ValidationUtils validationUtils
55+
56+ @Autowired
57+ ErrorController errorController
58+
59+ @Autowired
60+ ValidationValidator validationValidator
61+
62+ @Autowired
63+ ValidationHandler validationHandler
64+
4065 @Autowired
4166 @Subject
4267 private SampleController sampleController
4368
4469 @SpringBean
4570 UserService userService = Stub (UserService . class)
4671
47- private TestRestTemplate template = new TestRestTemplate ()
72+ @SpringBean
73+ UserRepository userRepository = Stub (UserRepository . class)
74+
75+ @Autowired
76+ private TestRestTemplate restTemplate
4877
4978 void setup (){}
5079
5180 @Test
52- def " if find user by id is ok" (){
81+ def " if getAll request return ok" (){
5382
54- given :" create mock userModel "
55- UserModel user = generateUser ()
83+ given :" create mock userModelList "
84+ def list = generateUserList ()
5685
57- and :" define behavior of userService.getUser method"
58- userService. getUser(_ ) >> user
86+ and :" define behavior of userService.getAll method"
87+ userService. getAll( ) >> list
5988
6089 when :" call endpoint"
61- def code = template. exchange(" http://localhost:9090/api/v1/user/10" , HttpMethod . GET , null , ResponseEntity<?> . class). statusCode
90+ def exchange = restTemplate. exchange(" /api/v1/user/all" ,
91+ HttpMethod . GET ,
92+ null ,
93+ new ParameterizedTypeReference<List<UserModel > > () {
94+ })
6295
6396 then :" expected return result"
64- code . value () == " 200 "
97+ exchange . getStatusCode () == HttpStatus . OK
6598 }
6699
100+ @Test
101+ def " if create user and return model then ok" (){
102+
103+ given :" create mock user and userModel"
104+ def user = generateUser()
105+ def model = generateUserModel()
106+
107+ and :" define behavior of userRepository methods"
108+ userRepository. insert(_) >> user
109+ userRepository. getUserWithNationalCode(_) >> null
67110
68- private static UserModel generateUser () {
111+ and :" create entity request"
112+ HttpHeaders headers = new HttpHeaders ()
113+ headers. setContentType(MediaType . APPLICATION_JSON )
114+ HttpEntity<UserModel > request = new HttpEntity<> (model, headers)
115+
116+ when :" call endpoint"
117+ def response = restTemplate. postForEntity(" /api/v1/user/add" ,
118+ request,
119+ String . class)
120+
121+ then :" expected return result"
122+ response. getStatusCode() == HttpStatus . OK
123+ }
124+
125+
126+ private static UserModel generateUserModel () {
69127 Random random = new Random ()
70128 new UserModel (
71- id : random. nextInt(),
129+ id : random. nextInt(1000 ),
130+ version : null ,
131+ insertDate : Utils . getLocalTimeNow(),
132+ updateDate : null ,
133+ activeStatus : true ,
134+ firstName : " first" ,
135+ lastName : " last" ,
136+ userName : " sample" ,
137+ nationalCode : " 0014713225" ,
138+ mobile : " 09388773155" ,
139+ email : " pouyapouryaie@gmail.com" ,
140+ gender : " man"
141+ )
142+ }
143+
144+ private static User generateUser () {
145+ Random random = new Random ()
146+ new User (
147+ id : random. nextInt(1000 ),
148+ activeStatus : true ,
149+ insertDate : Utils . getTimestampNow(),
150+ updateDate : null ,
72151 userName : " sample" ,
73152 firstName : " first" ,
74153 lastName : " last" ,
@@ -78,4 +157,11 @@ class SampleApiTest extends Specification{
78157 )
79158 }
80159
160+ private static List<UserModel > generateUserList () {
161+ List<UserModel > userModels = new ArrayList<> ()
162+ UserModel userModel = generateUserModel()
163+ userModels. add(userModel)
164+ return userModels
165+ }
166+
81167}
0 commit comments