Spring启动测试配置不能很好地覆盖正常配置

时间:2017-07-09 07:30:46

标签: spring-boot spring-cloud netflix-eureka

我有一个Spring启动应用程序,配置如下:

的src /主/资源/ application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://10.254.102.12:1111/eureka/

在我的单元测试用例中,我想设置eureka实例名称并更改日志记录级别。 的的src /测试/资源/ application.yml:

eureka:
  instance:
    appname: item-client-test

logging:
  level: debug

但是当我添加这些额外设置时,这些以前的设置似乎不再起作用了。 eureka serviceUrl回退到默认的loclahost:8761,所以我的测试失败了。当我将自定义eureka serviceUrl添加到src/test/resources/application.yml时,我的测试进展顺利。

所以我很困惑这是一个春季启动的错误还是我没有使用正确的配置方式?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用两个配置文件,例如:

<强>的src /主/资源/ application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://10.254.102.12:1111/eureka/

<强>的src /主/资源/应用test.yml

eureka:
  instance:
    appname: item-client-test
  client:
    serviceUrl:
      defaultZone: http://test-server/eureka/
logging:
  level: debug

并使用SPRING_PROFILES_ACTIVE=test mvn test

运行测试

Spring Boot将查找application-#{environment}配置文件(可以这么说)并使用它,继承自默认(application.yml)配置文件