๐ ๋ฐฐ๊ฒฝ
ํ์๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ํตํฉ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ ๋ ๊ฐ ์ผ์ด์ค๋ง๋ค ํ ์คํธ ๋ฉ์๋๋ฅผ ์์ฑํ๊ณ ์คํํ๊ณ , ๋ฉ์๋ ์์ฑ์ด ์ ๋นํ ๋ง๋ฌด๋ฆฌ๋๋ฉด ํด๋์ค ๋ด์ ์๋ ๋ชจ๋ ํ ์คํธ ๋ฉ์๋๋ฅผ ์คํ์์ผ ๋ณด๋ ๋ฐฉ์์ผ๋ก ์งํํด ์๋ค.
๊ทธ๋ฌ๋ค๊ฐ ํ ์คํธ ์ฝ๋๊ฐ ์ด๋ ์ ๋ ์์ด ์์์ ๋ ์ ์ฒด ํ ์คํธ๋ฅผ ๋๋ ธ๋๋ฐ, ์๊ฐ์ด ๊ต์ฅํ ์ค๋ ๊ฑธ๋ฆฌ๋ ๊ฒ์ ํ์ธํ๊ณ ๋ ์ถฉ๊ฒฉ์ ๋ฐ์๋ค. ๊ทธ๋ ๊ฒ ์ ์ฒด ํ ์คํธ ์ฝ๋ ์ํ ์๋์ ๋ํด ๊ณ ๋ฏผ์ ํ๊ฒ ๋์๊ณ , Context Caching์ ๋ํด ์๊ฒ ๋์๋ค.
[ Context Caching ]
์คํ๋ง ํ ์คํธ ํ๋ ์์ํฌ๋ ApplicationContext๊ฐ ์์ฑ๋๋ฉด ์ด๋ฅผ ์บ์์ ์ ์ฅํ๊ณ , ๋ง์ฝ ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ผ๋ฉด ๋ค๋ฅธ ํ ์คํธ๋ฅผ ๋๋ฆด ๋ ์ด๋ฅผ ์ฌ์ฌ์ฉํ๋ค. ์ด์ฒ๋ผ ํ ์คํธ์์ ๋์ด ์ปจํ ์คํธ๋ฅผ ํ ์คํธ๋ง๋ค ์์ฑํ์ง ์๊ณ ์บ์ฑํด ๋ ์ปจํ ์คํธ๋ฅผ ์ฌ์ฌ์ฉํ๋ ๊ฒ์ Context Caching์ด๋ผ๊ณ ํ๋ค.
Context Caching์ ์ฌ์ฉํ๋ฉด ๋ง์ ์คํ๋ง ํตํฉ(Integration) ํ ์คํธ ์ฝ๋๋ฅผ ๋น ๋ฅด๊ฒ ์คํ์ํด์ผ๋ก์จ ์์ฐ์ฑ์ ์ฆ๊ฐ์ํจ๋ค๋ ์ฅ์ ์ด ์๋ค.
๊ทธ๋ผ Context๋ฅผ ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ ์ด๋ค ๊ฒ์ผ๊น?
๋ฐ๋ก, ํ ์คํธ๊ฐ ๋์ผํ ํ๊ฒฝ์ ์ ์งํ๋ ๊ฒฝ์ฐ์ด๋ค.
์คํ๋ง์ ๋์ผํ ํ๊ฒฝ์ด๋ผ๋ ๊ฒ์ ์ด๋ป๊ฒ ํ๋ณํ ๊น? ์ด๊ฒ์ Context Caching์์ cache key๊ฐ ๋ฌด์์ผ๋ก ๊ตฌ์ฑ๋๋์ง์ ๊ด๋ จ์ด ์๋ค. ์คํ๋ง ํ ์คํธ ์ปจํ ์คํธ ํ๋ ์์ํฌ๋ ๋ค์ ๊ตฌ์ฑ ๋งค๊ฐ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ปจํ ์คํธ cache key๋ฅผ ๊ตฌ์ฑํ๋ค.
- locations (from @ContextConfiguration)
- classes (from @ContextConfiguration)
- contextInitializerClasses (from @ContextConfiguration)
- contextCustomizers (from ContextCustomizerFactory) – this includes @DynamicPropertySource methods as well as various features from Spring Boot’s testing support such as @MockBean and @SpyBean.
- contextLoader (from @ContextConfiguration)
- parent (from @ContextHierarchy)
- activeProfiles (from @ActiveProfiles)
- propertySourceLocations (from @TestPropertySource)
- propertySourceProperties (from @TestPropertySource)
- resourceBasePath (from @WebAppConfiguration)
์ด ์ค์์ ๊ฐ์ฅ ์ฃผ์ํด์ผ ํ ์ ์ @MockBean, @SpyBean์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ฌ์ฌ์ฉ ์ฌ๋ถ์ ์ํฅ์ ๋ฏธ์น๋ค๋ ๊ฒ์ด๋ค.
์ฆ, @MockBean, @SpyBean์ ์ฌ์ฉํ๋ฉด ๋ค๋ฅธ ํ๊ฒฝ์ผ๋ก ์ธ์ํ์ฌ ์๋กญ๊ฒ ์ปจํ ์คํธ๊ฐ ๋จ๊ฒ ๋๋ค.
[ ํ์ฉ Tip ]
1. ํ ์คํธ ์ฝ๋ ์์ ํ์ฉ
๋์ผํ ํ๊ฒฝ์ ์ ์ง์ํค๋ ์ข์ ๋ฐฉ๋ฒ ์ค ํ๋๋ ๋ถ๋ชจ ํด๋์ค์ ๋์ผํ ์ค์ ์ ๋ฃ๊ณ ์์ํด์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์์ ํด๋์ค์์ ๋ฌด์ธ๊ฐ ๋ค๋ฅธ ํ๊ฒฝ ์ค์ ์ ํ์ง ์๋ ์ด์ ๋์ผํ ํ๊ฒฝ์ ๊ฐํธํ๊ฒ ์ ์งํ ์ ์์ด์ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ ๋ ํธ๋ฆฌํ๋ค.
2. @MockBean/@SpyBean ๋ํ ์์ ํ์ฉ
@MockBean ๋๋ @SpyBean์ด ํ์ํ๋ค๊ณ ํ๋จ๋๋ฉด, ์ฌ๋ฌ ํ ์คํธ ์ฝ๋์์ ํ์ํ @MockBean/@SpyBean์ ๋ชจ๋ ํ๋์ ์์ ํด๋์ค์ ์ ์ธํ๊ณ ํด๋น ํด๋์ค๋ฅผ ์์์์ผ์ ์ฌ์ฉํ๋ค.
์ ์ ํด๋์ค๊ฐ ์ปค์ง๊ฒ ์ง๋ง ํ์๋ Context Caching์ด ์ฃผ๋ ์ด์ ์ด ๋ ํฌ๋ค๊ณ ์๊ฐ๋๋ค.
REFERENCE
'Spring > Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ผ๋ฏธํฐ ๊ฐ์ ํ ์คํธํ๋ Mockito ArgumentCaptor (0) | 2024.10.01 |
---|---|
flatExtracting ํ์ฉํด์ ๋ด๋ถ ๋ฆฌ์คํธ ๋ด์ฉ ๊ฒ์ฆํ๊ธฐ (0) | 2023.12.03 |
JPA Auditing @createdDate ํ ์คํธ ์ฝ๋ ์์ฑํ๊ธฐ (0) | 2023.10.30 |
@TempDir ํ์ฉํด์ ํ์ผ ํ ์คํธ ์ฝ๋ ์์ฑํ๊ธฐ (0) | 2023.09.25 |
๋ฐ์ดํฐ๋ฒ ์ด์ค ํตํฉ ํ ์คํธ ์ฝ๋ ์์ฑ Tip (0) | 2023.05.06 |
๋๊ธ