Springcloudユニットテスト登録センター、構成センター、自動構成クラスの除外

4780 ワード

Springcloudユニットテスト登録センター、構成センター、自動構成クラスの除外
  • シーン
  • ベース
  • 登録センター
  • を無視
  • 構成センタ
  • は無視する.
  • その他
  • redis構成クラス
  • を除外する.
    シーン
    springcloudプロジェクトにユニットテストを書き込み、ユニットテストを実行してサービスを登録するなどのプロセスは、煩雑で意味がないため、無視を構成する必要があります.RedisAutoConfigurationなどの自動構成クラスも自動注入は必要ありません
    ベースクラス
    次のベースクラスを書き、その後のユニットテストでそのクラスを継承すればよい.
    @RunWith(SpringRunner.class)
    @ActiveProfiles(value = "junit")
    @SpringBootTest(classes = KshptApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @Rollback(value = true)
    @TestPropertySource(properties={
            "spring.autoconfigure.exclude=" +
            "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration," +
            "org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration"
    })
    public class BaseTest {
    }
    
    @ActiveProfiles(value = "junit")は、ユニットテストにのみ使用されるプロファイルを定義します.
    登録センターを無視
    私たちが定義した**アプリケーション-junit.yml(bootstrap-junit.yml)**での構成
    eureka:
      client:
        enabled: false
    

    登録センターは無視されます
    コンフィギュレーションセンターを無視
    モジュールがコンフィギュレーション・センターに使用される場合は、コンフィギュレーション・センターのコンフィギュレーション・アイテムを**application-junit.に移行します.yml(bootstrap-junit.yml)**で、構成
    spring:
      cloud:
        config:
          enabled: false
    

    コンフィギュレーションセンターは無視されます
    その他
    たとえばspring-sessionを無視すると、構成
    spring:
      session:
        store-type: none
    

    redis構成クラスの除外
    @TestPropertySource(properties={
            "spring.autoconfigure.exclude=" +
            "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration," +
            "org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration"
    })
    

    基本クラスでは、RedisAutoConfigurationとRedisRepositoriesAutoConfigurationを排除し、ユニットテスト時にredisに接続できないためにエラーが発生しないようにします.