You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成测试无法读取指定Profile的yml配置文件

Why Spring Boot Isn't Loading Your 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 the embedded profile 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:

  • @ActiveProfiles is 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.
  • @TestPropertySource is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:07:56