小编Hen*_*ing的帖子

在 SpringBootTest 中禁用使用 spring.config.import 激活的配置服务客户端

我有一个裸露的 Spring Boot 应用程序

@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

它通过以下内容连接到 Spring Cloud Config Serverapplication.yml

spring:
  application:
    name: client

  config:
    import: configserver:http://localhost:8888
Run Code Online (Sandbox Code Playgroud)

当配置服务器正在运行时,应用程序工作正常,而当服务器未运行时,应用程序会按预期失败。

我现在想@SpringBootTest为不依赖于正在运行的配置服务器的应用程序编写一个集成测试,甚至不尝试连接到它。

配置服务器关闭后,进行裸测试

@SpringBootTest
class ClientApplicationTests {
    @Test
    void contextLoads() {
    }
}
Run Code Online (Sandbox Code Playgroud)

失败并显示java.net.ConnectException: Connection refused,这是预期的。

当我尝试禁用配置客户端时

@SpringBootTest(properties = "spring.cloud.config.enabled=false")
Run Code Online (Sandbox Code Playgroud)

测试不会尝试连接到服务器,但失败并显示

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    ...
Caused by: java.lang.IllegalStateException: Unable to load config data from 'configserver:http://localhost:8888'
    at …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-cloud-config spring-boot-test

12
推荐指数
2
解决办法
9033
查看次数