RestApi ๋งŒ๋“ค๊ธฐ - Repository์ถ”๊ฐ€ํ•˜๊ธฐ (2)

๋ฐ˜์‘ํ˜•

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();

์ด์ œ ๋ถ€ํ„ฐ๋Š” ์„ธ์ด๋ธŒํ•œ ๊ฐ์ฒด์˜ ์•„์ด๋””๋กœ ๋ฐ›์•„์„œ ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

๊ทธ ๊ฒฐ๊ณผ๋Š”

์„ฑ๊ณต์ด๋‹ค.

 

๋Œ“๊ธ€

Designed by JB FACTORY