如何在SpringBoot集成测试中调用Liquibase初始化Oracle数据库?
SpringBoot + Oracle Testcontainers 集成 Liquibase 最小可运行示例
1. 依赖配置(pom.xml)
确保引入兼容的Spring Boot和Liquibase依赖,以Spring Boot 3.x + Liquibase 4.x为例:
<dependencies> <!-- Spring Boot Web 核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Oracle JDBC 驱动 --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc11</artifactId> <scope>runtime</scope> </dependency> <!-- Liquibase Spring Boot 集成包 --> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency> <!-- Testcontainers Oracle 测试依赖 --> <dependency> <groupId>org.testcontainers</groupId> <artifactId>oracle-xe</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot 测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2. 测试环境配置(application-test.yml)
配置Testcontainers动态数据库连接和Liquibase参数:
spring: datasource: url: jdbc:tc:oracle:18.4.0-xe:///testdb username: test password: test liquibase: enabled: true change-log: classpath:db/changelog/db.changelog-master.xml default-schema: TEST
注:
jdbc:tc:oracle:...是Testcontainers专属URL格式,会自动启动Oracle容器并注入连接信息
3. Liquibase 变更日志文件
在src/main/resources/db/changelog下创建主日志文件db.changelog-master.xml:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd"> <include file="db/changelog/changes/001-create-users-table.xml"/> </databaseChangeLog>
在src/main/resources/db/changelog/changes下创建具体变更脚本001-create-users-table.xml:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd"> <changeSet id="001" author="your-name"> <createTable tableName="USERS"> <column name="ID" type="NUMBER(19,0)" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="USERNAME" type="VARCHAR2(50)"> <constraints nullable="false" unique="true"/> </column> <column name="EMAIL" type="VARCHAR2(100)"> <constraints nullable="false" unique="true"/> </column> </createTable> </changeSet> </databaseChangeLog>
4. 验证测试类
编写测试类确认Liquibase自动执行变更:
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest(properties = "spring.profiles.active=test") public class LiquibaseIntegrationTest { @Autowired private JdbcTemplate jdbcTemplate; @Test void testLiquibaseApplied() { // 验证USERS表存在且初始行数为0 Long count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM TEST.USERS", Long.class); assertEquals(0, count); } }
5. 手动调用Liquibase Update(编程式触发)
如果自动集成失效,可通过Spring的DataSource手动调用Liquibase API(适配Liquibase 4.24+版本):
import liquibase.Liquibase; import liquibase.database.Database; import liquibase.database.DatabaseFactory; import liquibase.database.jvm.JdbcConnection; import liquibase.resource.ClassLoaderResourceAccessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.sql.Connection; @Component public class LiquibaseManualRunner { private final DataSource dataSource; @Autowired public LiquibaseManualRunner(DataSource dataSource) { this.dataSource = dataSource; } public void runUpdate() throws Exception { try (Connection connection = dataSource.getConnection()) { Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); Liquibase liquibase = new Liquibase("db/changelog/db.changelog-master.xml", new ClassLoaderResourceAccessor(), database); liquibase.update(""); } } }
常见问题排查
- 确认
spring.liquibase.enabled为true,多环境配置时要检查对应profile的配置 - 核对变更日志文件路径,Liquibase默认读取
classpath:db/changelog/db.changelog-master.xml,路径不同需在配置中指定 - 查看启动日志,Spring Boot会打印Liquibase执行日志,如
Liquibase: Update has been successful,无日志说明Liquibase未被触发 - 检查Testcontainers Oracle容器是否正常启动,网络问题可能导致镜像拉取失败,查看Testcontainers相关日志
内容的提问来源于stack exchange,提问作者Martin Mucha
相关产品推荐
相关产品推荐

