使用Spring云的Spring引导:gradle构建失败
使用Spring云的Spring引导:gradle构建失败,spring,gradle,spring-boot,spring-cloud,spring-cloud-config,Spring,Gradle,Spring Boot,Spring Cloud,Spring Cloud Config,/gradlew build在运行:test任务时失败,错误出现在底部。代码只是检查上下文是否正确加载 @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class RegistryApplicationTests { @Test public void contextLoads() { } } 下面给出了bootstrap.yml文件(相当标准),我不确定它为什么试图从
/gradlew build
在运行:test
任务时失败,错误出现在底部。代码只是检查上下文是否正确加载
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class RegistryApplicationTests {
@Test
public void contextLoads() {
}
}
下面给出了bootstrap.yml文件(相当标准),我不确定它为什么试图从云配置服务加载属性文件,我该如何处理它
spring:
application:
name: registry
profiles:
active: default
cloud:
config:
uri: http://localhost:8888
fail-fast: true
eureka:
instance:
prefer-ip-address: true
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
堆栈跟踪
Caused by: java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:130)
at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:89)
at
....
....
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
at
....
....
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at
更新
根据@dzatorsky的建议,他尝试添加@Profile(“test”)
和@ActiveProfiles(“test”)
,不起作用
尝试使用@TestPropertySource(locations=“file:src/test/resources/application test.yml”)
手动添加测试的属性文件无效
最后,使用@TestPropertySource(properties={“spring.cloud.config.fail fast=false”})覆盖代码。
有效,但这看起来是一个非常难看的解决方法
推断是src/main/resources
中的bootstrap.yml
覆盖了在其他任何地方指定的属性,尝试将src/test/resources中的application-test.yml重命名为bootstrap.yml
已生效
这是更干净的方法吗
获取“”请求时出错:连接被拒绝:连接;嵌套异常为java.net.ConnectException:连接被拒绝
您的Spring云配置服务器似乎已关闭
更新:如果您希望在不运行配置服务器的情况下运行测试(在大多数情况下这是正确的做法),那么我建议您执行以下操作:
添加具有以下内容的application-test.yml:
cloud:
config:
fail-fast: false
用以下内容注释您的测试类:
@Profile("test")
在这种情况下,无论何时运行测试,它们都会使用application.yml中定义的默认参数加上application.test.yml中覆盖的参数。这是在执行:test.Change
fail fast
时为测试将true
属性更改为true。我已根据上面的注释更新了答案。