Spring-boot default profile for integration tests
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot significantly simplifies the process of building standalone, production-ready Spring applications. One of the conveniences it offers is its robust profile support. A "profile" in Spring Boot is a way to segregate parts of your application configuration, thereby making it possible to tailor the application behavior in different environments like development, testing, and production.
When it comes to integration testing, Spring Boot provides a default profile to streamline the testing process. This article delves into the details of using Spring Boot's default profile for integration tests, offering technical explanations and examples to help developers harness this feature effectively.
What is an Integration Test?
Integration tests evaluate whether different modules or services within an application work together as expected. They are intermediate between unit tests, which test individual components, and end-to-end tests, which examine complete workflows. Integration tests typically involve external components like databases, message brokers, or external APIs.
Spring Boot Profiles
Spring Boot profiles allow you to include or exclude configuration based on the provided application environment. Profiles can be specified in properties (application-{profile}.properties or application-{profile}.yaml) or through annotations, and can be activated via command-line arguments or environment variables.
Default Profile for Integration Tests
Spring Boot features a default mechanism for activating a different profile during tests. When using the @SpringBootTest annotation in your tests, the test profile is automatically activated unless you specify otherwise. This means you can have a separate application-test.properties file with configurations exclusively for testing.
Key Characteristics
- Isolation: Integration tests can run in an isolated environment, ensuring that tests do not affect the development or production databases.
- Custom Configuration: Define properties such as in-memory database settings, mock services, or other test-specific configurations.
- Automatic Activation: The
testprofile is automatically activated when the tests are executed, making it simple to set up.
Example Test Configuration
Suppose you are using an H2 in-memory database for tests, you might have a src/test/resources/application-test.properties file like this:
Writing a Simple Integration Test
The following example shows a basic Spring Boot integration test:
Deactivating the Default Profile
Although the test profile is suitable for most scenarios, you can deactivate it or switch to another profile by using the @ActiveProfiles annotation:
Advantages of Using Default Profiles
- Consistency: Keeps test and main application configurations separate, reducing accidental overlaps.
- Simplicity: Automatic activation eliminates the need for manual profile toggling.
- Flexibility: Customizable properties allow for numerous different test scenarios without altering the core source configuration files.
Table of Key Points
| Feature | Description |
| Default Profile | The test profile |
| Automatic Activation | Activated with @SpringBootTest |
| Isolation | Uses separate configs for tests |
| Customization | Test-specific configurations |
| Deactivation | Possible with @ActiveProfiles |
Additional Considerations
Testcontainers
If your application depends on more complex dependencies like databases, you might want to use tools like Testcontainers to spin up external resources required for integration tests dynamically. This can be managed within the test profile configuration to ensure repeatability and consistent test environments.
Mocking Frameworks
To further ensure isolation, you could integrate mocking frameworks like Mockito for substituting real service calls with predefined responses.
Conclusion
Utilizing Spring Boot's default profile for integration tests can significantly enhance the quality and maintainability of your test suite. By leveraging this feature, developers can ensure that their applications remain robust and reliable across different environments. Whether you're using in-memory databases or more advanced integrations, Spring Boot profiles provide the flexibility needed to streamline the testing process.
Related reading
- Spring-Boot execute data.sql in one profile only
- spring-boot Fatal error compiling invalid target release 17
- spring-boot gradle plugin can't be found
- spring-boot health not showing details withDetail info
- Spring Async - no data found in integration test
- Spring boot 1.4 Testing Configuration error found multiple declarations of BootstrapWith
- Spring-Boot How do I set JDBC pool properties like maximum number of connections?
- Spring-Boot How to properly inject javax.validation.Validator

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.