Reputation: 532
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)
Upvotes: 5
Views: 2832
Reputation: 3373
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.
Upvotes: 0
Reputation: 1047
@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.
Upvotes: 4