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

TIL, WIL/TIL๐Ÿ“˜

05. 02 TIL : RestDocs ์ ์šฉ ์‹ค์Šต

728x90

ํ•™์Šต ์ž๋ฃŒ ์ถœ์ฒ˜ : https://spring.io/guides/gs/testing-restdocs/

 

์Šคํ”„๋ง ๊ณต์‹ ๊ฐ€์ด๋“œ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์‹ค์Šต ํ”„๋กœ์ ํŠธ : https://github.com/spring-guides/gs-testing-restdocs

 

์ฐธ๊ณ ํ•œ ๊ธ€ : https://techblog.woowahan.com/2597/

 

ํ•™์Šต ๋ชฉํ‘œ : Spring ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ API๋ฅผ JUnit๊ณผ MockMvc ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ  RestDocs๋ฅผ ํ™œ์šฉํ•ด API ๋ฌธ์„œํ™”

 

์™œ Rest Docs?

- ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฌธ์„œ๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์œผ๋ฏ€๋กœ Rest Docs ๋ฌธ์„œํ™”๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋ฐ ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ธฐ๋Šฅํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ API์˜ ์‹ ๋ขฐ๋„๋ฅผ ๋†’์ด๊ณ  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค.  

 

1. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ ๋ฐ ์‹คํ–‰

1) HomeController.java ์ƒ์„ฑ

2) TestingRestdocsApplication.java 

package com.example.testingrestdocs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestingRestdocsApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestingRestdocsApplication.class, args);
	}
}

* ๋˜์ƒˆ๊ฒจ๋ณด๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ ๋นŒ๋“œ ์‹œ ์‚ฌ์šฉํ•˜๋Š” ์ฃผ์„

@SpringBootApplication : ๋‹ค์Œ์˜ ์ฃผ์„์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ํŽธ์˜ ์ฃผ์„

- @Configuration : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋นˆ์„ ์ •์˜ํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค์— ๋Œ€ํ•ด Spring Boot๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์ฃผ์„ ํ‘œ์‹œ

- @EnableAutoConfiguration : ํด๋ž˜์Šค ๊ฒฝ๋กœ ์„ค์ • ๋“ฑ spring.factories ๋‚ด๋ถ€ ์„ค์ •๋“ค์ด ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ ์šฉ์ด ๋˜์–ด ๋นˆ์ด ์ƒ์„ฑ์ด ๋˜๊ณ , ์Šคํ”„๋ง ๋ถ€ํŠธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.

- @EnableWebMvc : ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์›น์—์„œ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก Spring Mvc ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. spring์˜ DispatcherServlet ๋“ฑ๋ก, ViewResolver ์„ค์ •, HandlerMapping ๋“ฑ๋ก, MessageConverter ํ™œ์„ฑํ™”, StaticResourceHandler ๋“ฑ๋ก

https://cdaosldk.tistory.com/54

 

์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜ ๋…ธํŠธ ์ •๋ฆฌ (1) : ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์„ค์ •

1. with Spring initializr Metadata Group : ๊ธฐ์—… ์ด๋ฆ„, ๋„๋ฉ”์ธ ๋ช… Artifact : ๋นŒ๋“œ๋˜์–ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฌผ์˜ ์ด๋ฆ„ ( ํ”„๋กœ์ ํŠธ๋ช… ) Dependencies : ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ์—์„œ ์–ด๋–ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๊ฐ€? ์›น ํ”„๋กœ์ 

cdaosldk.tistory.com

- @ComponentScan : @Component ๋ฐ ์Šคํ…Œ๋ ˆ์˜ค ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ€์—ฌ๋œ ํด๋ž˜์Šค๋ฅผ ์Šค์บ”ํ•ด ๋นˆ์œผ๋กœ ๋“ฑ๋ก

2. ํ…Œ์ŠคํŠธ ์ž๋™ํ™”

1) Junit ๋ฐ Spring Rest Docs ์ข…์†์„ฑ ์ถ”๊ฐ€ : @MockMvc ์‚ฌ์šฉ

2) ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ

3) @Service๋Š” Mock ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

4) Controller Test ์ž‘์„ฑ

package com.example.testingrestdocs;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(HomeController.class)
// apply(documentationConfiguration(restDocumentation))์„ค์ • ์ž๋™ํ™”
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class WebLayerTest {

	@Autowired
	private MockMvc mockMvc;

	@Test
	public void shouldReturnDefaultMessage() throws Exception {
		this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
				.andExpect(content().string(containsString("Hello, World")))
				.andDo(document("home"));
	}
}

5) ์Šค๋‹ˆํŽซ์ด ์ƒ์„ฑ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

6) ํ…Œ์ŠคํŠธ ํ›„ asciidoctor ์‹คํ–‰ ์„ค์ • ~ asciidoctor๋Š” adoc ํŒŒ์ผ์„ html ๋“ฑ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋„๊ตฌ

 

3. ๋ฌธ์„œ ์ž‘์„ฑ

1) ์ƒ์„ฑ๋œ adoc ์Šค๋‹ˆํŽซ์„ ๋ชจ์•„ .adoc ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

2) asciidoctor๋ฅผ ์‹คํ–‰ํ•ด html ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

3) jar์— ๋ฌธ์„œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์Šคํ”„๋ง ๋ถ€ํŠธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นŒ๋“œ ์‹œ html ๋ฌธ์„œ๋ฅผ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

728x90

'TIL, WIL > TIL๐Ÿ“˜' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

05. 18 TIL  (0) 2023.05.18
05. 17 TIL  (0) 2023.05.17
04. 28 TIL  (0) 2023.04.28
04. 13 TIL  (0) 2023.04.13
04. 08 TIL  (0) 2023.04.08