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