SPRING BOOT單元測試中,因?yàn)闇y試時可能對應(yīng)的服務(wù)器地址不同于SIT等別的環(huán)境,通常會將這些地址放于application-sit.yaml中。
在單元測試的代碼中用這個標(biāo)簽指定用哪個profile,如
@ActiveProfiles({"embedded-mongodb","test"})
但這樣做法,由于@ActiveProfiles這個標(biāo)簽是final的,如果要測試別的profile,只能復(fù)制另一份單元測試代碼,再改此標(biāo)簽。
比較靈活的做法是用default profile,default profile是如果沒指定任何profile,則會默認(rèn)用這個。在application-default.yaml中再指定需激活的profile。
spring:
profiles:
active: test,embedded-mongodb
如果要測試別的profile,可以指定環(huán)境變量的方式覆蓋:
-Dspring.profiles.active=error,embedded-mongodb
為了安全起見,將application-default.yaml放在測試目錄中:src\test\resources。
Setting default Spring profile for tests with override option
https://blog.inspeerity.com/spring/setting-default-spring-profile-for-tests-with-override-option/