SpringBootTest不导入本地属性

问题描述

我正在运行完整的应用程序测试,并希望从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
}

配置文件(MyApp-test.yaml):

test-config:
  myPropertyValue: "testValue"
  myPropertyMap:
    mapKey: "myMapTestValue"

但是,当我尝试运行它时,出现此错误:

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...