【发布时间】:2019-01-26 20:57:54
【问题描述】:
我正在使用 Spring boot2,我正在尝试使用 H2 + Liquibase + JUNIT 配置单元测试。
我认为 liquibase 没有执行 changeLog 文件并应用 SQL 命令,单元测试无法识别我的表。
我在我的文件中放了错误的 sql 以查看更改日志文件是否已执行,但是,似乎没有执行。
为什么我的应用程序无法访问表?也许 liquibase 没有执行?
在我的 src/test/resource 我有这个文件:application.yml
spring:
application:
name: my-api
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:myDB;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password: sags
liquibase:
enabled: true
user: sa
password: sags
change-log: classpath:/db/changelog/db.changelog-master.xml
jpa:
hibernate:
ddl-auto: none
database-platform: org.hibernate.dialect.H2Dialect
show-sql: true
properties:
hibernate:
use_sql_comments: true
format_sql: true
h2:
console:
enabled: true
path: /console
我的测试课:
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FooRepositoryTest {
@Autowired
private FooRepository fooRepository;
@Test
public void findAllMustReturnAnything() {
List<Object> result = fooRepository.tables();
}
}
FooRepository 方法:
@Query(value = "SHOW TABLES", nativeQuery = true)
List<Object> tables();
当我运行单元测试时,我得到以下结果:
我的变更日志文件位于:src/main/resources/db
更新:我发现为什么 liquibase 没有执行我的 sql 代码。这是因为我的 SQL 文件有“dbms:mysql”,所以,我使用 H2 liquibase 执行的方式将不适用。
-- changeset 1.0:2 dbms:mysql
command
-- rollback command
现在,我需要知道为什么我在会话中选择的数据库不是 myDB。
【问题讨论】:
-
如果您的变更日志文件位于 src/main/resources 中,则其位置不是
classpath:/db/changelog/db.changelog-master.xml。它是classpath:db.changelog-master.xml(假设这是正确的文件名) -
对不起,我忘了在解释中加上“db”,我的文件在:src/main/resources/db 当我更改文件名以测试是否会失败时,它失败了,所以文件在执行过程中被发现,但什么也不做。
标签: spring-boot junit h2 liquibase