簡體   English   中英

使用來自 JUnit 測試的遠程配置啟動 Spring Boot 上下文

[英]Start Spring Boot context with remote config from JUnit Test

我正在微服務環境中編寫集成測試。 我想在運行我的 JUnit 測試類時完全初始化我的 Spring Boot 應用程序上下文一次。 在正常情況下,我可以很容易地做到這一點,但在這種情況下,我需要從 Git 存儲庫加載遠程配置(我使用的是 spring-cloud-config)。 在正常啟動條件下,這個 Spring Boot 應用程序確實成功地從 Git 存儲庫加載了配置。 我只是想在測試階段復制相同的上下文初始化。 這是我編寫測試類的方式:

@RunWith(SpringRunner.class)
@SpringBootTest(properties={"ENV=local","spring.cloud.config.enabled=true","spring.cloud.config.uri=http://config.server.com/config","ENCRYPT_KEY=secret"})
@ContextConfiguration(classes ={MyAppConfig.class,MyDBConfig.class})
@PropertySource({"classpath:spring-cloud-config.properties"})
public class MyIntegrationTest {

    @Test
    public void testFindUsersForCorp() {
        System.out.println("Test is running ");
    }
}

spring-cloud-config.properties

spring.profiles.active=${ENV}
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
spring.cloud.consul.host=${CONSUL.HOST:localhost}
spring.cloud.consul.port=${CONSUL.PORT:8500}
spring.cloud.consul.discovery.healthCheckPath=/custom/health
spring.cloud.consul.discovery.healthCheckInterval=30s
spring.cloud.config.uri=${CONFIG.SERVER.URI:http://config.server:80/config}
spring.cloud.config.username=user
spring.cloud.config.password=${PASSWORD}

在 Spring 上下文初始化期間記錄輸出:

main WARN: Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/userdata-service/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
main WARN: Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
main ERROR: 

***************************
APPLICATION FAILED TO START
***************************

Description:

Cannot determine embedded database driver class for database type NONE
[OMITTED]

如上所示,啟動失敗是因為我的持久層沒有定義驅動程序類。 由於相關配置在 Git 中,此錯誤表示未獲取遠程配置。

我感謝任何人可以提供的任何幫助。 如果需要,我會用更多信息更新這個問題。

Spring Cloud Config 客戶端應該在 bootstrap 階段初始化,因此您應該將相應的屬性從spring-cloud-config.properties移動到bootstrap.properties參見https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client。 html#config-first-bootstrap

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM