Spring Boot
Integration Testing
Default Profile
Java
Testing Configuration

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.

Browse interview questions

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.

java
@Profile("test")

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

  1. Isolation: Integration tests can run in an isolated environment, ensuring that tests do not affect the development or production databases.
  2. Custom Configuration: Define properties such as in-memory database settings, mock services, or other test-specific configurations.
  3. Automatic Activation: The test profile 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:

properties
1spring.datasource.url=jdbc:h2:mem:testdb
2spring.datasource.driverClassName=org.h2.Driver
3spring.datasource.username=sa
4spring.datasource.password=password
5
6# Hibernate
7spring.jpa.hibernate.ddl-auto=create-drop
8spring.h2.console.enabled=true

Writing a Simple Integration Test

The following example shows a basic Spring Boot integration test:

java
1@RunWith(SpringRunner.class)
2@SpringBootTest
3public class MyServiceIntegrationTest {
4
5    @Autowired
6    private MyService myService;
7
8    @Test
9    public void testServiceMethod() {
10        String result = myService.performOperation();
11        assertEquals("expectedResult", result);
12    }
13}

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:

java
1@RunWith(SpringRunner.class)
2@SpringBootTest
3@ActiveProfiles("integration")
4public class CustomProfileIntegrationTest {
5    // Your test cases
6}

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

FeatureDescription
Default ProfileThe test profile
Automatic ActivationActivated with @SpringBootTest
IsolationUses separate configs for tests
CustomizationTest-specific configurations
DeactivationPossible 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.