SpringBootTest:如何使用application-test.yaml并从application.yaml继承缺少的值

时间:2019-10-25 13:07:32

标签: spring spring-boot spring-boot-test

我正在尝试使用注释进行集成测试:

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles({ "test" })

我遇到的问题是应用程序通过使用弹簧配置文件“ test”加载 我有两个配置文件: application.yaml application-test.yaml

application.yaml包含:

xyz:
  list:
  - class-name: com.any.prod.ClassName1
    jndi-name: com/ws/ClassName1
  - class-name: com.any.prod.ClassName2
    jndi-name: com/ws/ClassName2

,另一个文件不包含这些值。 这些估值器由我使用的库使用,而不是由我的应用程序直接使用。

当我在启用“测试”配置文件的情况下加载测试时,未从application.yaml中获取值。如果我在application-test.yaml中添加相同的值,则它们将在绑定过程中被拾取。

这些是拾取的属性:

xyz.list[0].class-name: com.any.prod.ClassName1 (loaded from application-test.yaml)
xyz.list[0].jndi-name: com/ws/ClassName1 (loaded from application-test.yaml)
xyz.list[1].class-name: com.any.prod.ClassName2 (loaded from application-test.yaml)
xyz.list[1].jndi-name: com/ws/ClassName2 (loaded from application-test.yaml)
xyz.list (loaded from application.yaml)

不幸的是,最后一个条目使属性验证失败。 有谁想到我该怎么做才能解决这个谜? 至此,我对配置yaml文件是如何错误的理解(至少对于测试用例-部署应用程序时,继承似乎工作得很好)

1 个答案:

答案 0 :(得分:0)

您可以通过使用@TestPropertySource注释来实现。