The default profile is always active. Spring Boot loads all properties in application. yml into the default profile. We could rename the configuration file to application-default.
Spring Boot allows to define profile specific property files in the form of application-{profile}. properties . It automatically loads the properties in an application. properties file for all profiles, and the ones in profile-specific property files only for the specified profile.
The @SpringBootTest annotation loads the complete Spring application context. In contrast, a test slice annotation only loads beans required to test a particular layer. And because of this, we can avoid unnecessary mocking and side effects.
As far as I know there is nothing directly addressing your request - but I can suggest a proposal that could help:
You could use your own test annotation that is a meta annotation comprising @SpringBootTest
and @ActiveProfiles("test")
. So you still need the dedicated profile but avoid scattering the profile definition across all your test.
This annotation will default to the profile test
and you can override the profile using the meta annotation.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ActiveProfiles
public @interface MyApplicationTest {
@AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
}
Another way to do this is to define a base (abstract) test class that your actual test classes will extend :
@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("staging")
public abstract class BaseIntegrationTest {
}
Concrete test :
public class SampleSearchServiceTest extends BaseIntegrationTest{
@Inject
private SampleSearchService service;
@Test
public void shouldInjectService(){
assertThat(this.service).isNotNull();
}
}
This allows you to extract more than just the @ActiveProfiles
annotation. You could also imagine more specialised base classes for different kinds of integration tests, e.g. data access layer vs service layer, or for functional specialties (common @Before
or @After
methods etc).
You could put an application.properties file in your test/resources folder. There you set
spring.profiles.active=test
This is kind of a default test profile while running tests.
A delarative way to do that (In fact, a minor tweek to @Compito's original answer):
spring.profiles.active=test
in test/resources/application-default.properties
.test/resources/application-test.properties
for tests and override only the properties you need.Come 2021 and Spring Boot 2.4 the solution I have found is to have 3 properties files
src/main/resources/application.yml
- contains the application's default propssrc/test/resources/application.yml
- sets the profile to 'test', and imports properties from 'main'src/test/resources/application-test.yml
- contains test-specific profiles, which will override 'main'Here is the content of src/test/resources/application.yml
:
# for testing, set default profile to 'test'
spring.profiles.active: "test"
# and import the 'main' properties
spring.config.import: file:src/main/resources/application.yml
For example, if src/main/resources/application.yml
has the content
ip-address: "10.7.0.1"
username: admin
and src/test/resources/application-test.yml
has
ip-address: "999.999.999.999"
run-integration-test: true
Then (assuming there are no other profiles)...
when running tests,
profiles=test
--
ip-address=999.999.999.999
username=admin
run-integration-test=true
and when running the application normally
profiles=none
--
ip-address=10.7.0.1
username=admin
run-integration-test <undefined>
Note: if src/main/resources/application.yml
contains spring.profiles.active: "dev"
, then this won't be overwritten by src/test/resources/application-test.yml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With