Test Without @Autowire
@TestConstructor
์ ๋์ ์๋ฆฌ
@TestConstructor
์ ๋์ ์๋ฆฌ@TestConstructor
๋ Spring Boot์์ ํ
์คํธ ํด๋์ค์ ์์ฑ์๋ฅผ ํตํด ์์กด์ฑ์ ์ฃผ์
ํ ์ ์๋๋ก ์ ๊ณตํ๋ ์ ๋
ธํ
์ด์
์
๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก Spring Boot๋ ํ
์คํธ ํด๋์ค์์ ์์ฑ์ ์ฃผ์
์ ํ์ฑํํ์ง ์์ต๋๋ค. ํ์ง๋ง @TestConstructor
๋ฅผ ์ฌ์ฉํ๋ฉด ์์ฑ์ ์ฃผ์
์ ํ์ฑํํ๊ณ , ์ด๋ฅผ ํตํด ์์กด์ฑ์ ํ
์คํธ ํด๋์ค์ ์ ๋ฌํ ์ ์์ต๋๋ค.
1. AutowireMode
์ ์ญํ
@TestConstructor
๋ ๋ด๋ถ์ ์ผ๋กAutowireMode
๋ฅผ ํตํด ์์กด์ฑ ์ฃผ์ ๋ฐฉ์์ ๊ฒฐ์ ํฉ๋๋ค.AutowireMode.ALL
: ์์ฑ์์ ๋ชจ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์๋์ผ๋ก ์ฃผ์ ํฉ๋๋ค.ํ ์คํธ ์ปจํ ์คํธ์์ ํ ์คํธ ํด๋์ค์ ์์ฑ์๋ฅผ ์ค์บํ๊ณ , ํ์ํ ์์กด์ฑ์ ApplicationContext์์ ๊ฐ์ ธ์ต๋๋ค.
์คํ๋ง ์ปจํ ์ด๋์์ ๊ด๋ฆฌ๋๋ ๋ชจ๋ Bean์ด ์ฃผ์ ๊ฐ๋ฅํฉ๋๋ค.
How do Code?
๋์ ํ๋ฆ
TestContextManager
๊ฐExampleTest
๋ฅผ ์์ฑ์ ๊ธฐ๋ฐ์ผ๋ก ์ด๊ธฐํ.SpringBootTestContextBootstrapper
๊ฐMyService
์MyRepository
๋ฅผ ์คํ๋ง ์ปจํ ์คํธ์์ ๊ฒ์.์์ฑ์ ํธ์ถ ์ ๊ฐ ์์กด์ฑ์ ์ฃผ์ ํ ๊ฐ์ฒด๋ฅผ ์์ฑ.
ํ ์คํธ ๋ฉ์๋(
testService
) ์คํ.
TIP
Spring Boot 2.4 ์ด์
@TestConstructor
๋ Spring Boot 2.4 ์ด์์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋ค๋ฅธ ์์กด์ฑ ์ฃผ์ ๋ฐฉ์๊ณผ์ ์ถฉ๋
@Autowired
๋ฅผ ํตํด ํ๋ ์ฃผ์ ์ด๋ ๋ฉ์๋ ์ฃผ์ ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์ ํผ์ฉํ๋ฉด ํผ๋์ ์ด๋ํ ์ ์์ผ๋ฏ๋ก, ํ๋์ ์ฃผ์ ๋ฐฉ์์ ์ผ๊ด๋๊ฒ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
@TestConstructor ์์ด๋ ๋์ ๊ฐ๋ฅ
application.properties
์ ์๋ ์ค์ ์ ์ถ๊ฐํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์์ฑ์ ์ฃผ์ ์ ํ์ฑํํ ์ ์์ต๋๋ค:
Last updated
Was this helpful?