繁体   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