๐ 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 |