0
0
Spring Bootframework~10 mins

Test profiles and configuration in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test profiles and configuration
Start Application
Check Active Profile
Load application-{profile}.properties
Override default config
Run Tests with Profile
Use Profile-specific Beans
Tests Execute with Configured Settings
End
The application starts, checks which profile is active, loads the matching configuration, overrides defaults, and runs tests using those settings.
Execution Sample
Spring Boot
@SpringBootTest
@ActiveProfiles("test")
class MyServiceTest {
  @Autowired
  MyService service;
  @Test
  void testMethod() {
    assertEquals("testValue", service.getConfigValue());
  }
}
This test runs with the 'test' profile active, so it uses test-specific configuration values.
Execution Table
StepActionProfile ActiveConfig LoadedBean UsedTest Output
1Start Applicationnoneapplication.propertiesdefault beansN/A
2Activate 'test' profiletestapplication-test.propertiestest beans if anyN/A
3Load ConfigurationstestDefaults overridden by test configtest beansN/A
4Run testMethod()testapplication-test.propertiestest beansservice.getConfigValue() returns 'testValue'
5Assert resulttestapplication-test.propertiestest beansPass: 'testValue' matches expected
6End Testtestapplication-test.propertiestest beansTest completes successfully
💡 Test finishes after asserting the expected value with the test profile configuration.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
activeProfilenonetesttesttesttest
configSourceapplication.propertiesapplication-test.propertiesapplication-test.propertiesapplication-test.propertiesapplication-test.properties
beanTypedefaulttesttesttesttest
service.getConfigValue()N/AN/AN/AtestValuetestValue
Key Moments - 2 Insights
Why does the test use 'application-test.properties' instead of 'application.properties'?
Because the '@ActiveProfiles("test")' annotation activates the 'test' profile, causing Spring Boot to load 'application-test.properties' which overrides the default config as shown in execution_table step 2 and 3.
How does Spring Boot know which beans to use for the test profile?
Spring Boot loads beans annotated or configured for the active profile. In the execution_table steps 3 and 4, 'test beans' are used instead of default beans because the 'test' profile is active.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what value does 'service.getConfigValue()' return?
A"null"
B"testValue"
C"defaultValue"
D"productionValue"
💡 Hint
Check the 'Test Output' column at step 4 in the execution_table.
At which step does the application load the profile-specific configuration?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the 'Config Loaded' column in the execution_table to find when overrides happen.
If the '@ActiveProfiles' annotation was removed, what would change in the variable_tracker?
AbeanType would be 'test'
BactiveProfile would remain 'test'
CconfigSource would be 'application.properties'
Dservice.getConfigValue() would return 'testValue'
💡 Hint
Refer to the 'activeProfile' and 'configSource' rows in variable_tracker to see what changes without profile activation.
Concept Snapshot
Test Profiles and Configuration in Spring Boot:
- Use @ActiveProfiles("profileName") to activate a profile in tests.
- Spring Boot loads application-{profile}.properties overriding defaults.
- Beans and configs change based on active profile.
- Tests run with profile-specific settings for isolation.
- Helps separate dev, test, prod configs cleanly.
Full Transcript
In Spring Boot, test profiles let you run tests with special settings. When the application starts, it checks which profile is active. If the 'test' profile is active, it loads 'application-test.properties' to override default settings. Beans specific to the test profile are used. This way, tests run with configurations made just for testing. The example code shows a test class with '@ActiveProfiles("test")' which activates the test profile. The execution table traces how the profile activates, config files load, beans switch, and the test method returns the expected test value. Variables like activeProfile and configSource change to reflect the test profile. This helps keep test settings separate from production or development. Understanding when and how profiles load helps avoid confusion about which configs and beans are used during tests.