![[์ฝ์ง๋ก] ๋ฏผ๊ฐ ์ ๋ณด์ ํ๊ฒฝ๋ณ์ ์นํ ํ ํ
์คํธ ์คํจ ํ์](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FVxiwU%2FbtsJkvQb3Ol%2FGvqOAoKKgd1xC4ct8557j0%2Fimg.png)
[์ฝ์ง๋ก] ๋ฏผ๊ฐ ์ ๋ณด์ ํ๊ฒฝ๋ณ์ ์นํ ํ ํ
์คํธ ์คํจ ํ์Back-end/Error2024. 8. 30. 12:06
Table of Contents
๐ ๋ฌธ์ ์ํฉ
// application.yaml
spring:
datasource:
url: ${LOCAL_DB_URL}
username: ${LOCAL_DB_USERNAME}
password: ${LOCAL_DB_PASSWORD}
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
}
}
ํ๊ฒฝ๋ณ์๋ฅผ ์ด์ฉํด application.yaml์์ ์๋ ๋ฏผ๊ฐ ์ ๋ณด๋ฅผ ์นํํ์ฌ ๋ณด์์ฑ์ ๋์๋๋ฐ, ์ผ๋ถ ์คํ๋ง ๋ถํธ ํ ์คํธ๊ฐ ์คํจํ๋ค.
๋ฌธ์ ์์ธ
@SpringBootTest ๋ ์ด๊ธฐ ํ ์คํธ ํ๊ฒฝ์ ๊ตฌ์ฑํ๋ฉด์ ํ๋กํผํฐ ํ์ผ์ ์ค์ ์ ์ฐธ๊ณ ํ๊ธฐ ๋๋ฌธ
⇒ ํ๊ฒฝ๋ณ์๊ฐ ์ฃผ์ ๋์ง ์์ ํ ์คํธ ํ๊ฒฝ์์ @SpringBootTest ๊ฐ ์ด๋ฅผ ๊ทธ๋๋ก ์ฝ์ผ๋ฉด์ ์ฌ๋ฐ๋ฅธ jdbc url ํฌ๋งท์ด ์๋๋ฏ๋ก ์คํจ
โ ํด๊ฒฐ
ํ ์คํธ์์ ๋ค๋ฅธ ํ๋กํ์ผ์ ์ฌ์ฉํ์
๋ฏผ๊ฐ์ ๋ณด๋ฅผ ํ๊ฒฝ๋ณ์๋ก ๊ฐ์ถ๋ฉด์ ํ ์คํธ๋ฅผ ์ ์ ๋์ํ๊ฒ ํ๊ธฐ ์ํด์
์ธ๋ฉ๋ชจ๋ฆฌ ํ ์คํธ DB์ธ h2 ๊ฒฝ๋ก๋ฅผ ์๋ก ๋ง๋ ํ ์คํธ ์ ์ฉ test ํ๋กํ์ผ์ ์ง์ ํด์ฃผ๊ณ ,
ํ ์คํธ ์คํ ์ ์ด ํ๋กํ์ผ์ ๋ฐ๋ผ๋ณด๊ฒ ํ๋ฉด ๋จ.
// application.yaml ํ๋จ์ ์ถ๊ฐ
---
spring:
config.activate.on-profile: test
datasource.url: jdbc:h2:mem:testdb
@ActiveProfiles("test")
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
}
}
@ActiveProfiles
- ์คํ๋ง์์ ํ ์คํธ ์คํ ์ ํน์ ํ๋กํ์ผ์ ํ์ฑํํ์ฌ ํ ์คํธ๋ฅผ ์ํํ๋๋ก ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ด๋ ธํ ์ด์
- ์์ ์ํฉ์์ ApplicationTests ํด๋์ค์์ @ActiveProfiles("test")
→ test ํ๋กํ์ผ์ ํ์ฑํ ํ๋๋ก ์ง์ ํจ
→ ์ด ํ๋กํ์ผ์ ํ ์คํธ ์คํ ์ ์คํ๋ง ์ปจํ ์คํธ์ ๋ก๋๋์ด application.yaml ํ์ผ์์ ํด๋น ํ๋กํ์ผ์ ๋ง๋ ์ค์ ์ด ์ ์ฉ๋จ
→ ํ๋กํ์ผ๋ณ๋ก ๋ค๋ฅธ ํ๊ฒฝ์์์ ๋์์ ํ ์คํธํ ์ ์๋ค.