Java SpringBootTest没有';不导入本地属性
我正在运行完整的应用程序测试,希望从Java SpringBootTest没有';不导入本地属性,java,spring-boot,junit,spring-boot-test,Java,Spring Boot,Junit,Spring Boot Test,我正在运行完整的应用程序测试,希望从src/test/resources读取我的应用程序的所有属性,但当我添加@TestPropertySource和位置时,应用程序仍然无法加载 在生产环境中,我的配置托管在一个外部服务器上,我能够在boostrap.yml:spring.cloud.config.uri:{pathToDevelopmentConfigServer}中成功加载本地测试的配置,但是,当我为了尝试本地读取配置而删除配置时,我无法加载应用程序 我的设置: 测试等级: @SpringB
src/test/resources
读取我的应用程序的所有属性,但当我添加@TestPropertySource
和位置时,应用程序仍然无法加载
在生产环境中,我的配置托管在一个外部服务器上,我能够在boostrap.yml:spring.cloud.config.uri:{pathToDevelopmentConfigServer}
中成功加载本地测试的配置,但是,当我为了尝试本地读取配置而删除配置时,我无法加载应用程序
我的设置:
测试等级:
@SpringBootTest
@EnableConfigurationProperties
@TestPropertySource(locations = {"classpath:MyApp.yaml", "classpath:MyApp-test.yaml"})
@ActiveProfiles(profiles = {"default", "test"})
@ContextConfiguration(classes = {MyAppProperties.class})
public class MyAppTestClass {
@Test
public void myTest(){
assertTrue(true); //dummy test just to have one for now
}
}
配置类:
@Configuration
@ConfigurationProperties(prefix = "test-config")
@Validated
public class MyAppProperties {
@NotNull
private String myPropertyValue;
@NotNull
private Map<String, String> myPropertyMap;
// Getters & Setters
}
但是,当我尝试运行此操作时,会出现以下错误:
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under '-test' to com.c.taylor.properties.MyAppProperties$$EnhancerBySpringCGLIB$$fd769f1a failed:
Property: test-config.myPropertyValue
Value: null
Reason: must not be null
Property: test-config.myPropertyMap
Value: null
Reason: must not be null
Action:
Update your application's configuration
我读过几篇关于如何将属性加载到SpringBootTest中的文档,只是运气不好
提前谢谢
编辑: src/main/resources/bootstrap.yml文件:
---
spring:
application:
name: MyApp
cloud:
config:
uri: {pathToDevelopmentConfigServer}
enabled: false
这里可能不需要TestPropertySource。在“src/test/resources”中创建简单的“application.yaml”。删除所有其他yaml文件和TestPropertySource注释。它应该可以正常工作。删除@TestPropertySource并将所有配置移到src/test/resources中的MyApp.yaml文件中。行为没有改变
---
spring:
application:
name: MyApp
cloud:
config:
uri: {pathToDevelopmentConfigServer}
enabled: false