11package ir.bigz.springbootreal.web
22
3+ import com.fasterxml.jackson.databind.ObjectMapper
34import 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
75import ir.bigz.springbootreal.controller.SampleController
86import ir.bigz.springbootreal.dal.UserRepository
97import 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
148import ir.bigz.springbootreal.service.UserService
15- import ir.bigz.springbootreal.service.UserServiceImpl
16- import ir.bigz.springbootreal.validation.*
17- import ir.bigz.springbootreal.validation.annotation.Validator
189import ir.bigz.springbootreal.viewmodel.UserModel
1910import org.junit.jupiter.api.Test
2011import org.spockframework.spring.SpringBean
@@ -23,47 +14,33 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration
2314import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
2415import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
2516import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
26- import org.springframework.boot.test.context.SpringBootTest
27- import org.springframework.boot.test.web.client.TestRestTemplate
28- import org.springframework.context.ApplicationContext
29- import org.springframework.core.ParameterizedTypeReference
30- import org.springframework.http.*
31- import org.springframework.test.context.ContextConfiguration
17+ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
18+ import org.springframework.context.annotation.ComponentScan
19+ import org.springframework.http.HttpEntity
20+ import org.springframework.http.HttpHeaders
21+ import org.springframework.http.HttpStatus
22+ import org.springframework.http.MediaType
23+ import org.springframework.test.web.servlet.MockMvc
24+ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
25+ import org.springframework.test.web.servlet.setup.MockMvcBuilders
3226import org.springframework.transaction.annotation.EnableTransactionManagement
27+ import org.springframework.web.context.WebApplicationContext
3328import spock.lang.Specification
34- import spock.lang.Subject
3529import spock.lang.Title
3630
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])
4231@Title (" sample controller mock test" )
43- @SpringBootTest (properties = " spring.profiles.active:test" , webEnvironment = SpringBootTest.WebEnvironment .RANDOM_PORT )
4432@EnableAutoConfiguration (exclude = [DataSourceAutoConfiguration . class,
4533 HibernateJpaAutoConfiguration . class,
4634 DataSourceTransactionManagerAutoConfiguration . class])
4735@EnableTransactionManagement
36+ @ComponentScan (" ir.bigz.springbootreal" )
37+ @WebMvcTest (properties = " spring.profiles.active:test" )
4838class SampleApiTest extends Specification {
4939
5040 @Autowired
51- ApplicationContext applicationContext
41+ private WebApplicationContext webApplicationContext
5242
5343 @Autowired
54- private ValidationUtils validationUtils
55-
56- @Autowired
57- ErrorController errorController
58-
59- @Autowired
60- ValidationValidator validationValidator
61-
62- @Autowired
63- ValidationHandler validationHandler
64-
65- @Autowired
66- @Subject
6744 private SampleController sampleController
6845
6946 @SpringBean
@@ -72,10 +49,13 @@ class SampleApiTest extends Specification{
7249 @SpringBean
7350 UserRepository userRepository = Stub (UserRepository . class)
7451
75- @Autowired
76- private TestRestTemplate restTemplate
52+ private MockMvc mockMvc
7753
78- void setup (){}
54+ void setup (){
55+ this . mockMvc = MockMvcBuilders
56+ .webAppContextSetup(webApplicationContext)
57+ .build()
58+ }
7959
8060 @Test
8161 def " if getAll request return ok" (){
@@ -87,14 +67,14 @@ class SampleApiTest extends Specification{
8767 userService. getAll() >> list
8868
8969 when :" call endpoint"
90- def exchange = restTemplate . exchange( " /api/v1/user/all " ,
91- HttpMethod . GET ,
92- null ,
93- new ParameterizedTypeReference< List< UserModel > > () {
94- } )
70+
71+ def result = mockMvc . perform( MockMvcRequestBuilders . get( " /api/v1/user/all " )
72+ .contentType( MediaType . APPLICATION_JSON )
73+ .accept( MediaType . APPLICATION_JSON ))
74+ .andReturn( )
9575
9676 then :" expected return result"
97- exchange . getStatusCode() == HttpStatus . OK
77+ result . getResponse() . getStatus() == HttpStatus . OK . value()
9878 }
9979
10080 @Test
@@ -108,18 +88,19 @@ class SampleApiTest extends Specification{
10888 userRepository. insert(_) >> user
10989 userRepository. getUserWithNationalCode(_) >> null
11090
111- and :" create entity request"
112- HttpHeaders headers = new HttpHeaders ()
113- headers. setContentType(MediaType . APPLICATION_JSON )
114- HttpEntity<UserModel > request = new HttpEntity<> (model, headers)
91+ and :" create json Object"
92+ ObjectMapper objectMapper = new ObjectMapper ()
93+ def json = objectMapper. writeValueAsString(model)
11594
11695 when :" call endpoint"
117- def response = restTemplate. postForEntity(" /api/v1/user/add" ,
118- request,
119- String . class)
96+ def expect = mockMvc. perform(MockMvcRequestBuilders . post(" /api/v1/user/add" )
97+ .contentType(MediaType . APPLICATION_JSON )
98+ .content(json)
99+ .accept(MediaType . APPLICATION_JSON ))
100+ .andReturn()
120101
121102 then :" expected return result"
122- response . getStatusCode() == HttpStatus . OK
103+ expect . getResponse() . getStatus() == HttpStatus . CREATED . value()
123104 }
124105
125106
0 commit comments