RestApi ๋ง๋ค๊ธฐ - Repository์ถ๊ฐํ๊ธฐ (2)
- ๊ฐ๋ฐ/restAPI๐ข
- 2021. 2. 6. 20:20

c accept,content์ ๋ํด ๋ฌด์งํ๋๊ฒ ๊ฐ๋ค.
@Test
void create_Delivery() throws Exception {
Delivery delivery = Delivery.builder()
.id(10)
.item("book")
.user("klom")
.build();
mockMvc.perform(post("/api/delivery/")
.accept(MediaTypes.HAL_JSON_VALUE)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(delivery))
)
.andDo(print())
.andExpect(status().isCreated());
}
์ฝ๋๋ฅผ ์ด๋ ๊ฒ ์์ ํ๋ค.
ํ์ฌ ์ฝ๋๋ ๋ชจ๋ MediaTypes๋ฅผ ํ์ฉํ๊ธฐ ๋๋ฌธ์ ์ฑ๊ณตํ๊ฒ ๋๋ค.
@RequestMapping(value = "/api/delivery" ,produces = MediaTypes.HAL_JSON_VALUE)
๊ทผ๋ฐ ์ฌ๊ธฐ์ ๋๋ ์๋ฌธ์ ์ ์ด๊ฒ์ ๋ฃ์ง ์๋๋ค๋ฉด ์ด๋ค์์ผ๋ก ๋์ค๋์ง ๊ถ๊ธํด์ก๋ค.
์ปจํธ๋กค๋ฌ, ํ ์คํธ ๋๋ค ์๋ ๊ฒฝ์ฐ
MockHttpServletRequest:
์์ฒญ
HTTP Method = POST
Request URI = /api/delivery/
Parameters = {}
Headers = [Content-Length:"94"]
Body = {"id":10,"item":"book","user":"klom","deliveryTime":null,"deliveryEndTime":null,"status":null}
Session Attrs = {}
MockHttpServletResponse:
์๋ต
Status = 201
Error message = null
Headers = [Location:"http://localhost/api/delivery/%257Bid%257D", Content-Type:"application/json"]
Content type = application/json
Body = {"id":10,"item":null,"user":null,"deliveryTime":null,"deliveryEndTime":null,"status":null}
Forwarded URL = null
Redirected URL = http://localhost/api/delivery/%257Bid%257D
Cookies = []
์์ง๊น์ง๋ ๋ค๋ฅธ๊ฒ์ด ํ์ธ์ด ๋์ง ์๋๋ค.
์ปจํธ๋กค๋ฌ์๋ง ์๋ ๊ฒฝ์ฐ
MockHttpServletRequest:
์์ฒญ
HTTP Method = POST
Request URI = /api/delivery/
Parameters = {}
Headers = [Content-Length:"94"]
Body = {"id":10,"item":"book","user":"klom","deliveryTime":null,"deliveryEndTime":null,"status":null}
Session Attrs = {}
MockHttpServletResponse:
์๋ต
Status = 201
Error message = null
Headers = [Location:"http://localhost/api/delivery/%257Bid%257D", Content-Type:"application/hal+json"]
Content type = application/hal+json
Body = {"id":10,"item":null,"user":null,"deliveryTime":null,"deliveryEndTime":null,"status":null}
Forwarded URL = null
Redirected URL = http://localhost/api/delivery/%257Bid%257D
Cookies = []
content type์ ์ฃผ๋ชฉํด๋ณด๋ฉด ๋ณ๊ฒฝ์ด ๋๊ฒ์ ํ์ธ ํ ์ ์๋ค.
ํ ์คํธ์๋ง ์๋ ๊ฒฝ์ฐ
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/delivery/
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/hal+json", Content-Length:"94"]
Body = {"id":10,"item":"book","user":"klom","deliveryTime":null,"deliveryEndTime":null,"status":null}
Session Attrs = {}
MockHttpServletResponse:
Status = 201
Error message = null
Headers = [Location:"http://localhost/api/delivery/%257Bid%257D", Content-Type:"application/hal+json"]
Content type = application/hal+json
Body = {"id":10,"item":null,"user":null,"deliveryTime":null,"deliveryEndTime":null,"status":null}
Forwarded URL = null
Redirected URL = http://localhost/api/delivery/%257Bid%257D
Cookies = []
content-type์ด ์๋ก ๋ค๋ฅด๋ค๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
์ด๊ฒ์ ๋ค์ ์๊ฐํด๋ณด๋ฉด
์ปจํธ๋กค๋ฌ์์ ํน์ ์์ฒญ์ ์์ฒญํ๋ค.
๊ทธ๋ฌ๋ฉด ๊ทธ๊ฒ์ ํ
์คํธ์์ ์์ฒญ๋ ๊ฒ์ด ์์ฑ๋์ด์ง๋๋ฐ,
์ปจํธ๋กค๋ฌ๋ ๊ทธ ์์ฒญ์ ๋ฐ์์ ์๋ต์ผ๋ก ๋ง๋ค์ด์ค๋ค.
์ฆ, ์ปจํธ๋กค๋ฌ -> ํ
์คํธ -> ์ปจํธ๋กค๋ฌ ์์ผ๋ก ์งํ๋๋ค๊ณ ์๊ฐํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
์๋ฌด๋ฆฌ ์์์ ์๋ฌ๊ฐ ๋์ง ์๋๋ค๊ณ ํด๋ ์์ฒญ๊ณผ ์๋ต์ ์๋ก ์ผ์นํด์ฃผ๋๊ฒ ์ข์ ๋ฐฉ๋ฒ์ด๋ผ ์๊ฐํ๋ค.
๋์ค์ ๋์ฒํ๋ ค ํ๋ฉด ๋๋ฉ์ด ์ฒ๋ผ ๋ถ์ด๋์ ๋ชปํ ์ง๋ ๋ชจ๋ฅธ๋ค.
๊ทธ๋์ ์ด๋ฒ์๋ ์ด๋ ๊ฒ ์์ ํ๋ค.
์ ๋ถ ์๋ ๊ฒฝ์ฐ
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/delivery/
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/hal+json", Content-Length:"94"]
Body = {"id":10,"item":"book","user":"klom","deliveryTime":null,"deliveryEndTime":null,"status":null}
Session Attrs = {}
MockHttpServletResponse:
Status = 201
Error message = null
Headers = [Location:"http://localhost/api/delivery/%257Bid%257D", Content-Type:"application/hal+json"]
Content type = application/hal+json
Body = {"id":10,"item":null,"user":null,"deliveryTime":null,"deliveryEndTime":null,"status":null}
Forwarded URL = null
Redirected URL = http://localhost/api/delivery/%257Bid%257D
Cookies = []
ํ์ธ๊ฒฐ๊ณผ ์๋ก ์ผ์นํ๋ค๋ ๊ฒ์ ํ์ธ ํ ์ ์์๋ค.
์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํด๋ณด์.
์๊ฐํด๋ณด๋ฉด db๊ฐ ์ถ๊ฐ๋๋ค๋ฉด ์ด๋ป๊ฒ ๋ ์ง ์์ํด๋ดค๋ค.

์ด๋ฌํ ์ปดํ์ผ์๋ฌ๊ฐ ์๊ฒผ๋ค๋๊ฑด ์์ง ๋ง๋ค์ง ์์๋ค๋ ์ด์ผ๊ธฐ๋ค.
๋ง๋ค์ด ๋ณด์.
์์ฑํ ์ด๋๋ก ์คํํด๋ณด๋ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.

์ด๋ฌํ ์ด์ ๊ฐ ๋์ค๋ ๊ฒฐ์ ์ ์ธ ์ด์ ๋
ํ์ฌ webMvcTest๋ก ๋์ด์๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๊ฒ์ด ์ ์ค์ํ๋ ๊ทธ ์ด์ ๋ ์ด ํ
์คํธ๋ ์ฌ๋ผ์ด์ฑ ํ
์คํธ๋ผ๋ ๋ป์ด๋ค.
์ฌ๋ผ์ด์ฑ ํ
์คํธ๋ผ๋ ํด๋น ์กฐ๊ฐ๋ง ํ
์คํธ๊ฐ ๊ฐ๋ฅํ๋ ๋ป์ผ๋ก,
์ฌ๊ธฐ์์๋ web๊ด๋ จ ํ
์คํธ๋ง ๊ฐ๋ฅํ๋ค๋ ๋ป์ด๋๋ค.
ํ์ง๋ง ์์ฝ๊ฒ๋ Repository๋ web๊ด๋ จ ์ค์ ์ด ์๋๋ค. |
์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์กด์ฌํ๊ฒ ์ง๋ง,
๊ฐ๋จํ ํ๋ ๋ฐฉ๋ฒ์
MockBean์ผ๋ก ์ฃผ์
๋ฐ๋ ๊ฒ์ด๋ค.
์ฆ, ์ด๊ฒ์ ๊ฐ์์ผ๋ก ๋น์ผ๋ก ๋ง๋ค์ค๋ค๋ ๊ฑฐ๋ค.
@MockBean
private DeliveryRepository deliveryRepository;

์ด์ deliveryRepository๋ฅผ ์ฌ์ฉํด๋ณด์.
์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋ ๊น?
Delivery newDelivery = deliveryRepository.save(delivery);
๋ฌผ๋ก ์ด ๋ฐฉ๋ฒ๋ ํ๋ฆฐ ๋ฐฉ๋ฒ์ ์๋๋ค. ํ์ง๋ง ์ด ์ฝ๋์ ๊ฐ์ฅ ํฐ ๋ฌธ์ ์ ์ ๊ตณ์ด ์ด๋ฌํ ์ฝ๋๋ฅผ ํ
์คํธ์ ์์ฑํด์ผ ๋๋๋ ๊ฒ์ด๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผ๋ ๊น?
๋ง์ฝ์ save๊ฐ ๋ ๋ delivery๊ฐ ๋ฆฌํด์ด ๋๋ค๊ณ ํ๋ค๋ฉด?
์ด๋ฐ๊ฒ์ด ๋ ํ ์คํธ์ ์ด์ธ๋ฆฌ์ง ์์๊น?
Mockito.when(deliveryRepository.save(any(Delivery.class))).thenReturn(delivery);
when : ์ด๋ ํ ํ๋์ ํ๋ค๋ฉด,
๋ค์์ ๋ฆฌํดํ๋ค.
์์ฝ๊ฒ๋ ์๋ฌ๊ฐ ๋์ค์ง ์๋๋ค.
๊ทธ๋์ ์ด์ฉ ์ ์์ด ์ปจํธ๋กค๋ฌ์ ์ฝ๋๋ฅผ ์์ ํ์.
Delivery newDelivery = deliveryRepository.save(deliver);
URI createUri = linkTo(DeliveryController.class).slash(newDelivery.getId()).toUri();
์ด์ ๋ถํฐ๋ ์ธ์ด๋ธํ ๊ฐ์ฒด์ ์์ด๋๋ก ๋ฐ์์ ๊ทธ๊ฒ์ ์ฌ์ฉํ๋ ์ฝ๋๋ก ๋ง๋ค์๋ค.
๊ทธ ๊ฒฐ๊ณผ๋
์ฑ๊ณต์ด๋ค.

'๊ฐ๋ฐ > restAPI๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RestApi ๋ง๋ค๊ธฐ - spring-rest-docs (5) (0) | 2021.02.19 |
|---|---|
| RestApi ๋ง๋ค๊ธฐ - HATEOAS (5) (0) | 2021.02.16 |
| RestApi ๋ง๋ค๊ธฐ - badRequest (4) (0) | 2021.02.13 |
| RestApi ๋ง๋ค๊ธฐ - ์ ๋ ฅ๊ฐ ์ ํํ๊ธฐ + ์ ๋ ฅ๊ฐ ์ด์ธ์ ์๋ฌ ๋ฐ์ (3) (0) | 2021.02.09 |
| RestApi ๋ง๋ค๊ธฐ - 201์ฝ๋ ์์ฑํ๊ธฐ (1) (0) | 2021.01.30 |