Spring Boot集成测试无法读取指定Profile的yml配置文件
embedded Profile Configuration in Tests Let's break down why your test is failing to read the spring.datasource.url property from application-embedded.yml, even though logs confirm the embedded profile is active.
Core Issue: Invalid YAML Format
The most likely culprit here is incorrect indentation in your YAML file. YAML relies entirely on proper space-based nesting (tabs won't work!) to define hierarchical properties. Looking at your provided config, the structure is flattened instead of nested:
# Your original (invalid) format spring: h2: console: enabled: true liquibase: enabled: true ...
This means Spring Boot can't parse properties correctly—spring.datasource.url isn't recognized as a nested property under spring, so it doesn't exist in the application environment. That's why you get the "Required key 'url' not found" error.
Fixes to Try
1. Fix the YAML Indentation
Rewrite your application-embedded.yml with consistent nesting (use 2 or 4 spaces per level):
spring: h2: console: enabled: true liquibase: enabled: true change-log: classpath:db/changelog/db.changelog-master.xml drop-first: true # should be false in prod but good for dev and test! datasource: url: jdbc:h2:mem:testdb username: h2 password: h2 security: oauth2: resource: filter-order: 3 signing-key: MaYzkSjmkzPC57L security-realm: AvMaint Realm jwt: client-id: avmaintwebsitejwtclientid client-secret: XY7kmzoNzl100 grant-type: password scope-read: read scope-write: write resource-ids: avmaintwebsitejwtresourceid
Once indentation is corrected, Spring will properly map the properties, and env.getProperty("spring.datasource.url") should return the expected value.
2. Verify Configuration File Location
Ensure application-embedded.yml is placed in either:
src/main/resources(shared across main code and tests)src/test/resources(test-specific resources)
Maven/Gradle automatically includes these directories in the classpath during tests, so Spring Boot can find the file when theembeddedprofile is active.
3. Understand @ActiveProfiles vs @TestPropertySource
Adding @TestPropertySource("/application-embedded.yml") works, but it bypasses Spring Boot's profile-based configuration logic. Here's the key difference:
@ActiveProfilesis for environment isolation: you can define multiple profile-specific files (e.g.,application-dev.yml,application-prod.yml) and switch between them without code changes (via annotations, command-line args, or environment variables). This keeps configuration clean and environment-agnostic.@TestPropertySourceis for test-specific overrides: use it to tweak individual properties for a single test, not to load an entire environment's config. Using it to load a full profile file defeats the purpose of profiles, since you lose dynamic configuration switching.
Final Notes
After fixing the YAML indentation, your original test with @ActiveProfiles("embedded") should work as expected. Profiles are meant to simplify managing environment-specific configurations—don't abandon them just because a formatting issue broke things!
内容的提问来源于stack exchange,提问作者Michael Coxon

