๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SpringBoot & JPA

ํŒจ์ŠคํŠธ์บ ํผ์Šค ์ฑŒ๋ฆฐ์ง€ 10์ผ์ฐจ - JUnit & Mockito, RestTemplate

๐Ÿ“Œ JUnit & Mockito

โœ”๏ธ ๋นˆ์œผ๋กœ ๊ด€๋ฆฌ๋˜๋Š” ๊ฐ์ฒด์— ๋Œ€ํ•œ Mocking
@MockBean

Mockito
    .when(mock๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ)
    .thenReturn(๋ฆฌํ„ด๊ฐ’ ์ง€์ •);

โœ”๏ธ ์ปจํŠธ๋กค๋Ÿฌ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ( @MockMvc )

@WebMvcTest(ControllerName.class)
@AutoConfigureWebMvc
@Import({ClazzA.class ClazzB.class}) // ํ•„์š”ํ•œ ๋นˆ ๋“ฑ๋ก

@Autowired
private MockMvc mockMvc;

[ GET ]
mockMvc.perform(
    MockMvcRequestBuilders
        .get(url)
        .queryParam("key", "value")        
        ).andExpect(
            MockMvcResultMatchers.status().isOk()
        ).andExpect(
            MockMvcResultMatchers.content().string("Test")
        ).andDo(
            MockMvcResultHandlers.print();
        )



[ POST ]
mockMvc.perform(
    MockMvcRequestBuilders
        .post(url)
        .contentType(MediaType.)
        .content()
    ).andExpect(
        MockMvcResultMatchers.status().isOk();
    ).andExpect( ๐Ÿ‘‰ Json์— ์ ‘๊ทผ "$.key"
        MockMvcResultMatchers.jsonPath("$.key").value(expectValue)
    ).andExpect( ๐Ÿ‘‰ depth๊ฐ€ ๋” ์žˆ๋Š” Json์— ์ ‘๊ทผ "$.key1.key2"
        MockMvcResultMatchers.jsonPath("$.key1.key2").value(expectValue)
    ).andDo(
        MockMvcResultHandlers.print()
    );

โœ”๏ธ Jacoco
์ฝ”๋“œ ์ปค๋ฒ„๋ฆฌ์ง€ ํ™•์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

[Gradle]

plugins {
    ...
    id 'jacoco' 
}

๐Ÿ“Œ RestTemplate

โœ”๏ธ RestTemplate
์„œ๋ฒ„ ๊ฐ„ ํ†ต์‹ ์„ ์œ„ํ•œ ๊ฐ์ฒด

RestTemplate restTemplate = new RestTemplate();

โœ”๏ธ RestTemplate GET ์š”์ฒญ

getForObject(uri, ๋ฆฌํ„ดํƒ€์ž….class)
  • ์ง€์ •ํ•œ ๋ฆฌํ„ดํƒ€์ž…์„ ๋ฆฌํ„ด
getForEntity(uri, ๋ฆฌํ„ดํƒ€์ž….class)
  • ResponseEntity์— ์ง€์ •ํ•œ ๋ฆฌํ„ดํƒ€์ž…์„ ๋ฆฌํ„ด

โœ”๏ธ RestTemplate POST ์š”์ฒญ

postForObject(uri, ์š”์ฒญ๋ฐ์ดํ„ฐ, ๋ฆฌํ„ดํƒ€์ž….class);
  • ์ง€์ •ํ•œ ํƒ€์ž…์„ ๋ฆฌํ„ด
postForEntity(uri, ์š”์ฒญ๋ฐ์ดํ„ฐ, ๋ฆฌํ„ดํƒ€์ž….class)
  • ResponseEntity์— ์ง€์ •ํ•œ ํƒ€์ž…์„ ๋ฆฌํ„ด

โœ”๏ธ ํ—ค๋”๋ฅผ ํฌํ•จํ•œ HTTP ํ†ต์‹  (exchange)

  • RequestEntity๋ฅผ ์ „๋‹ฌ
RequestEntity 
    .post(uri) .contentType(MediaType.APPLIATION_JSON) 
    .header("headerName", "value") .header("headerName", "value") 
    .body(bodyData); exchange(requestEntity, ๋ฆฌํ„ดํƒ€์ž….class);`
  • ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ๋ฆฌํ„ดํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•  ๋•Œ exchange์˜ ๋ฆฌํ„ดํƒ€์ž…์œผ๋กœ RequestDto.class ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์—†์Œ ์• ์ดˆ์— ์ œ๋„ค๋ฆญ ํƒ€์ž…์—๋Š” .class ์‚ฌ์šฉ๋ถˆ๊ฐ€

RequestDto.class ๋Œ€์‹ ์— new ParameterizedTypeReference(){}; ์‚ฌ์šฉ

โœ”๏ธ UriComponentBuilder

UriComponentBuilder  
    .fromUriString("http://localhost:8080")
    .path("/api/user/save")
    .queryParam("name", "kim") // Query-String
    .queryParam("age", 10)
    .encode() // ์ธ์ฝ”๋”ฉ
    .build()
    .toUri();

http://localhost:8080/api/user/save?name=kim&age=10

  • URI ํƒ€์ž…์„ ๋ฆฌํ„ด
UriComponentBuilder
    .fromUriString("http://localhost:8080")
    .path("/api/user/save/{userId}")
    .encode()
    .build()
    .expand(1) // Path-variable (','๋กœ ๊ฐ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ๊ตฌ๋ถ„)
    .toUri();

=> http://localhost:8080/api/user/save/1
=> expand๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ ์šฉ๋จ

์•„๋ž˜์™€ ๊ฐ™์€ JSON ๋””์ž์ธ (body๋ถ€๋ถ„์€ ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ณ€๊ฒฝ๋จ)

{
    "header" : {
        status_code : "200"
    },
    "body" : {
        "name" : "kim",
        "age" : 10
    }
}
@Data
@AllArgsConstructor
public class RequestDto<T> {

    private Header header;
    private T body;


    @Data
    @AllArgsConstructor
    static class Header {
        private String status_code;

    }
}

๐Ÿ‘‰ API ์ŠคํŽ™์ด ๊ฐ™์€ ๋ผˆ๋Œ€๋ฅผ ๊ฐ–๋Š”๋‹ค๋ฉด ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•ด์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ


๐Ÿ‘ ์ˆ˜๊ฐ•์ธ์ฆ

 

 

ํŒจ์ŠคํŠธ์บ ํผ์Šค [์ง์žฅ์ธ ์‹ค๋ฌด๊ต์œก]

ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ์˜์ƒํŽธ์ง‘, UX/UI, ๋งˆ์ผ€ํŒ…, ๋ฐ์ดํ„ฐ ๋ถ„์„, ์—‘์…€๊ฐ•์˜, The RED, ๊ตญ๋น„์ง€์›, ๊ธฐ์—…๊ต์œก, ์„œ๋น„์Šค ์ œ๊ณต.

fastcampus.co.kr

๋ณธ ํฌ์ŠคํŒ…์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค ํ™˜๊ธ‰ ์ฑŒ๋ฆฐ์ง€ ์ฐธ์—ฌ๋ฅผ ์œ„ํ•ด ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

'SpringBoot & JPA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

SpringBoot & JPA API์„ค๊ณ„-2  (0) 2021.07.13
SpringBoot & JPA API์„ค๊ณ„-1  (0) 2021.07.13