Spring boot 如何在spring引导测试中重写application-test.yml?
我有src/main/test/resources/application-test.yml,根据SpringBootTest,它将加载application.yml,然后加载application-test.yml。但我面临这样一种情况:我只想为一个测试覆盖application-test.yml中的某些属性,而其他测试需要使用application-test.yml中的属性。我该怎么做 我试图使用@TestPropertySource注释来覆盖,但它不起作用Spring boot 如何在spring引导测试中重写application-test.yml?,spring-boot,spring-boot-test,Spring Boot,Spring Boot Test,我有src/main/test/resources/application-test.yml,根据SpringBootTest,它将加载application.yml,然后加载application-test.yml。但我面临这样一种情况:我只想为一个测试覆盖application-test.yml中的某些属性,而其他测试需要使用application-test.yml中的属性。我该怎么做 我试图使用@TestPropertySource注释来覆盖,但它不起作用 @Slf4j @RunWith(
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes= MyApplicationTestApplication.class)
@ActiveProfiles("test")
@DirtiesContext
@TestPropertySource(locations = {"classpath:application-test.yml",
"classpath:file-test.properties"})
再创建一个配置文件并激活它们(顺序很重要)
@ActiveProfiles({“test”,“test override”})
或者,您可以在spring上下文开始加载自身之前,使用System.properties(例如在静态块中)进行重写。谢谢您的评论和回答,只想添加对我有用的内容
@SpringBootTest(properties = "some.property=localhost:9094")
对于此
现在我想再次覆盖某些属性
您可以使用应用程序测试.yml
对吗?我已经在使用application-test.yml了。问题是如何覆盖application-test.yml中的属性,因为我需要只为我的一个测试覆盖某些属性。@TestPropertySource
覆盖所有其他属性。因此,只需将其添加到文件test.properties
中,并保留@ActiveProfile
。Spring将在此基础上引导一个新的上下文,并使用特殊设置。您可能不需要@DirtiesContext
,除非您希望在每次测试之前重新启动所有内容。@M.Deinum file-test.properties在Spring引导时不被考虑。我还需要使用DirtiesContext重新启动,因为每个测试模拟的bean都不同。为什么需要一个@DirtiesContext
@MockBean
实例已放置在上下文中。属性文件是否在类路径中?因为它确实应该被考虑在内。看起来你正在做一些不应该做的事情,从而破坏了引导代码。我不想再创建一个配置文件。但它还是奏效了。但是这是有效的。虽然这是有效的,但是有一个用于覆盖属性的配置文件,所以这对我来说是有效的@springbootest(properties=“some.property=localhost:9094”)