使用 Spring Boot Profile 实现 dev/test/staging 数据源切换。

解读

在国内互联网公司的自动化测试面试中,**“多环境数据源隔离”是高频考点。Selenium 脚本最终要接入 CI/CD,而 CI/CD 会对应多套部署环境(dev/test/staging)。如果测试代码里写死数据库地址,一旦环境迁移就会出现“脚本绿但数据错”的假阳性。Spring Boot Profile 提供了“一次打包、多环境运行”的官方方案,面试官想确认候选人是否能把 WebDriver 的 UI 自动化与后端数据断言无缝打通,同时保证“代码零改动、只改配置”**即可切换环境,这是落地并行分布式测试的前提。

知识点

  1. Spring Boot Profile 激活优先级:命令行 --spring.profiles.active > application-{profile}.properties > application.properties
  2. DataSource 自动配置:spring.datasource.url/username/password/driver-class-name 四要素
  3. @Profile 注解:加在 @Configuration 或 @Bean 上,实现 Bean 级条件装配
  4. @Primary 与 @Qualifier:多数据源时标记默认 Bean,避免 NoUniqueBeanDefinitionException
  5. Maven/Gradle 打包:使用 spring-boot-maven-plugin 的 profiles 标签,“一个 jar 包通吃三套环境”,符合国内运维“禁止二次编译”的规范
  6. 与 Selenium 结合:测试基类通过 @ActiveProfiles 指定环境,@BeforeSuite 中根据 Profile 动态拼接 Web 登录 URL 与数据库断言 SQL,实现“UI+DB”端到端一致性校验

答案

  1. 目录结构(符合国内阿里规约)
src/main/resources
├── application-dev.yml
├── application-test.yml
├── application-staging.yml
└── application.yml
  1. 三套环境配置示例(以 MySQL 为例)
    application-dev.yml
spring:
  datasource:
    url: jdbc:mysql://dev.db.example.com:3306/selenium_demo?useSSL=false&serverTimezone=Asia/Shanghai
    username: dev_user
    password: dev_pass
    driver-class-name: com.mysql.cj.jdbc.Driver

application-test.yml

spring:
  datasource:
    url: jdbc:mysql://test.db.example.com:3306/selenium_demo?useSSL=false&serverTimezone=Asia/Shanghai
    username: test_user
    password: Test#123
    driver-class-name: com.mysql.cj.jdbc.Driver

application-staging.yml

spring:
  datasource:
    url: jdbc:mysql://staging.db.example.com:3306/selenium_demo?useSSL=false&serverTimezone=Asia/Shanghai
    username: staging_user
    password: Stag@987
    driver-class-name: com.mysql.cj.jdbc.Driver

application.yml 公共配置

spring:
  profiles:
    active: dev   # 默认兜底,防止本地 IDEA 直接启动报错
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  1. 启动命令(与 Jenkins 流水线对齐)
# 开发自测
java -jar selenium-test.jar --spring.profiles.active=dev

# 测试环境 CI
java -jar selenium-test.jar --spring.profiles.active=test

# 预发布验收
java -jar selenium-test.jar --spring.profiles.active=staging
  1. 代码层零侵入
@SpringBootTest
@ActiveProfiles("test")   // 单元/集成测试强制走 test 库
public class BaseSeleniumTest {
    @Autowired
    protected DataSource dataSource; // Spring 已根据 Profile 注入对应数据源

    @BeforeSuite
    public void setUp() {
        // 根据 dataSource 的 metadata 拿到当前 URL,拼接域名
        String url = dataSource.getConnection().getMetaData().getURL();
        if (url.contains("test")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
            driver.get("https://test.example.com");
        }
    }
}
  1. 打包脚本(GitLab CI 示例)
mvn clean package -DskipTests -Pprod
# 产物:selenium-test.jar,内含 dev/test/staging 三套配置,**“一次构建,到处运行”**

拓展思考

  1. 敏感信息脱敏:国内等保 2.0 要求密码不能落盘,可把 spring.datasource.password=${DB_PWD} 占位,通过 Jenkins 凭据注入 + Kubernetes Secret 挂载,实现“配置与密文分离”
  2. 动态数据源:如果 Selenium 脚本需要在**“一次运行中轮询多环境”(如灰度对比),可用 AbstractRoutingDataSource + ThreadLocal 实现“运行时切换”**,但需考虑事务隔离级别,防止脏读
  3. 与 TestNG 并行结合:利用 TestNG 的 <parameter name="spring.profiles.active" value="test"/>,配合 Selenium Grid,**“Profile+浏览器双维度”**并行,可在 4C8G 节点上把三套环境回归从 2h 压缩到 15min
  4. 环境健康预检:在 @BeforeTest 中执行 SELECT 1,如果数据源连通失败直接**“快速失败”**,避免 Selenium 已经跑完 UI 才发现数据库断言超时,浪费 Grid 资源