paulwong

          SPRING BOOT單元測試之@ActiveProfiles

          SPRING BOOT單元測試中,因為測試時可能對應(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/




          posted on 2021-02-04 15:31 paulwong 閱讀(1832) 評論(0)  編輯  收藏 所屬分類: SPRING BOOT

          主站蜘蛛池模板: 新平| 浮山县| 常州市| 湘潭市| 福建省| 永和县| 浦县| 历史| 孝义市| 黔江区| 巴青县| 金塔县| 洞口县| 民勤县| 三河市| 淄博市| 镇平县| 珠海市| 华容县| 宜黄县| 扬州市| 巴马| 石阡县| 松阳县| 阿拉尔市| 青铜峡市| 雷州市| 专栏| 揭东县| 乌苏市| 浮梁县| 丹棱县| 民和| 分宜县| 昭平县| 西昌市| 高清| 林口县| 枝江市| 清新县| 安乡县|