Skip to content

springboot_test_configuration

choisungwook edited this page Oct 8, 2021 · 4 revisions

๊ฐœ์š”

  • sringboot ํ…Œ์ŠคํŠธ ์„ค์ •

์š”์•ฝ

  1. build.gradle ์„ค์ •
  2. ํ…Œ์ŠคํŠธ ์Šคํ”„๋งํ”„๋กœํŒŒ์ผ(application-test.yml)ํŒŒ์ผ ์ƒ์„ฑ
  3. ํ…Œ์ŠคํŠธ์ฝ”๋“œ์— @SpringbootTest ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •๊ณผ ํ…Œ์ŠคํŠธ ์Šคํ”„๋งํ”„๋กœํŒŒ์ผ ๋กœ๋“œ ์„ค์ •

gradle์„ค์ •

  • ์Šคํ”„๋ง ํ”„๋กœํŒŒ์ผ์„ ๋Ÿฐํƒ€์ž„ ์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก bootRun์„ค์ •
bootRun {
	String activeProfile = System.properties['spring.profiles.active']
	systemProperty "spring.profiles.active", activeProfile
}
  • h2, springboot test๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

ํ…Œ์ŠคํŠธ์ฝ”๋“œ

  • @SpringbootTest ์• ๋…ธํ…Œ์ด์…˜๊ณผ ์Šคํ”„๋งํ”„๋กœํŒŒ์ผ ์„ค์ •
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class AccountServiceTest {
    ...
}
  • mockmvc ์˜ˆ์ œ
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class AccountServiceTest {
    @Autowired
    private MockMvc mockMvc;

	...
}

์ฐธ๊ณ ์ž๋ฃŒ