【发布时间】:2019-08-17 10:50:45
【问题描述】:
我有 src/main/test/resources/application-test.yml,根据 SpringBootTest,它将加载 application.yml,然后加载 application-test.yml。但是我面临这样一种情况,我只想为一个测试覆盖 application-test.yml 中的某些属性,而其他测试需要使用 application-test.yml 中的属性。我该怎么做?
我尝试使用@TestPropertySource 注释来覆盖,但它不起作用。
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes= MyApplicationTestApplication.class)
@ActiveProfiles("test")
@DirtiesContext
@TestPropertySource(locations = {"classpath:application-test.yml",
"classpath:file-test.properties"})
【问题讨论】:
-
对于这个
Now again I want to override certain properties,你可以使用application-test.yml对吗? -
我已经在使用 application-test.yml 了。问题是如何覆盖 application-test.yml 中的属性,因为我需要只为我的一项测试覆盖某些属性。
-
@TestPropertySource覆盖其他所有内容。所以只需添加file-test.properties并留下@ActiveProfile。 Spring 将基于该上下文和特殊设置引导一个新上下文。您可能不需要@DirtiesContext,除非您希望在每次测试之前重新启动所有内容。 -
@M.Deinum file-test.properties 在 Spring 引导时不被考虑。还使用 DirtiesContext 因为我需要它重新启动,因为每个测试的模拟 bean 都不同。
-
你为什么需要
@DirtiesContext。@MockBean实例已放置在上下文中。属性文件在类路径中吗?因为它确实应该被考虑在内。看起来你正在做你不应该做的事情,因此会破坏引导代码。
标签: java spring-boot spring-boot-test