gpt4 book ai didi

java - @ActiveProfiles 和 @TestPropertySource 的区别

转载 作者:行者123 更新时间:2023-12-04 10:15:00 28 4
gpt4 key购买 nike

这两个注解有什么区别?

@ActiveProfiles("test")
@TestPropertySource({"classpath:/application-test.properties"})

我已经在同一个测试类上看到了这两个注释,据我所知,它们都导致 application-test.properties 被加载(覆盖主 application.properties 中的任何冲突属性)

最佳答案

@ActiveProfiles

@ActiveProfiles is a class-level annotation that is used to declare which bean definition profiles should be active when loading an ApplicationContext for an integration test.


从上面的定义我们可以理解,要在运行测试时激活配置文件而不指定文件的实际位置,我们使用此注释来加载该配置文件的属性。
然而 @TestPropertySource

@TestPropertySource is a class-level annotation that is used to configure the locations() of properties files and inlined properties() to be added to the Environment's set of PropertySources for an ApplicationContext for integration tests.

Test property sources have higher precedence than those loaded from the operating system’s environment or Java system properties as well as property sources added by the application declaratively through @PropertySource or programmatically. Thus, test property sources can be used to selectively override properties defined in system and application property sources. Furthermore, inlined properties have higher precedence than properties loaded from resource locations.


如果是 @TestPropertySource我们明确提到要从中加载属性的文件的位置。无需在此处激活任何配置文件。您正在测试类上对此进行注释,以便从特定位置的文件加载属性。它可以是任何属性,如常见属性等。
因此,在您的情况下,您可能只需要一个注释。我建议您删除 @ActiveProfiles 并进行测试,反之亦然。

关于java - @ActiveProfiles 和 @TestPropertySource 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61107829/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com