@ActiveProfiles和@TestPropertySource之间的区别是什么?

huangapple go评论55阅读模式
英文:

The difference between @ActiveProfiles and @TestPropertySource

问题

这两个注解之间有什么区别?

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

我在同一个测试类上看到了这两个注解,据我所知,它们都会导致加载application-test.properties文件(覆盖主应用程序.properties中的任何冲突属性)。

英文:

What is the difference between these two annotations?

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

I've seen both these annotations on the same test class and as far as I can tell they both result in the application-test.properties being loaded (overriding any conflicting properties from the main application.properties)

答案1

得分: 4

@ActiveProfiles
>@ActiveProfiles 是一个类级别的注解,用于在集成测试加载ApplicationContext时声明哪些bean定义配置文件应该处于活动状态。

从上述定义中我们可以理解,在运行测试时激活一个配置文件,而不指定实际文件位置,我们可以使用这个注解来加载该配置文件的属性。

而另一方面

@TestPropertySource

>@TestPropertySource 是一个类级别的注解,用于配置要添加到集成测试ApplicationContext的Environment的PropertySources集的locations()属性文件和内联属性()。
>
>测试属性源的优先级高于从操作系统环境或Java系统属性加载的属性源,以及通过@PropertySource或编程方式从应用程序声明添加的属性源。因此,测试属性源可用于有选择地覆盖系统和应用程序属性源中定义的属性。此外,内联属性的优先级高于从资源位置加载的属性。

@TestPropertySource的情况下,我们明确指定要从中加载属性的文件的位置。这里不需要激活任何配置文件。您可以在测试类上注释此内容,以便从特定位置的文件加载属性。可以是任何属性,例如常用属性等。

因此,在您的情况下,您可能只需要一个注解。我建议您删除@ActiveProfiles并进行测试,反之亦然。

英文:

@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.

From the above definition we can understand that to activate a profile while running Tests without specifying the actual location of the file we use this annotation to load properties for that profiles.

whereas

@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.

In case of @TestPropertySource we explicitly mention the location of the file from which you want to load the properties. There is no need of activating any profile here. You are annotating this on your test class so as to load the properties from a file at particular location.It can be any properties like common properties etc.

So In your case you may only need one annotation.I would suggest you to remove @ActiveProfiles and test and vice versa.

答案2

得分: 0

与通过在测试中使用@ActiveProfiles("test")来激活整个配置文件相比,对我来说@TestPropertySource真正带来价值的地方是来自于文档中的下面这句话:

因此,测试属性源可用于选择性地覆盖系统和应用程序属性源中定义的属性。

换句话说,我们可以重用已经定义的所有属性,只关注我们在测试期间想要具有不同值的特定属性。我们尽量不要过多地涉及应用程序上下文,因为每个应用程序上下文的定制都会使其与在生产环境中启动的“真实”应用程序上下文不同。

英文:

To me what really brings value to @TestPropertySource when compared to activating a whole profile for tests with @ActiveProfiles("test") is the statement below from the documentation:

> Thus, test property sources can be used to selectively override properties defined in system and application property sources.

In other words, we can reuse all of the already defined properties and focus only on the specific properties we want to have different values during our tests. The less we fiddle with the application context, the better as each customization of the application context is one more thing that makes it different from the "real" application context that is started up in a production setting.

huangapple
  • 本文由 发表于 2020年4月9日 02:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定