首页 > 解决方案 > 无法使用全局引用的标识符运行 springboot junit 测试

问题描述

我有一个运行spring.jpa.properties.hibernate.globally_quoted_identifiers=true. 数据库更改通过 liquibase 管理,应用程序使用 mysql 数据库。问题是,当我尝试运行使用 hsqldb 的 junit 测试时,在进行存储库调用时出现以下异常:

Caused by: java.sql.SQLSyntaxErrorException: unexpected token:  in statement

我相信这是因为我们目前没有设置默认模式,所以它在没有模式的情况下运行它,但是 liquibase 在PUBLIC模式中创建了表。我尝试spring.liquibase.default-schema=SCHEMA在我的测试属性中设置与我的产品属性相同的模式,但随后 liquibase 得到

liquibase.exception.DatabaseException: invalid schema name: SCHEMA in statement [CREATE TABLE SCHEMA.DATABASECHANGELOGLOCK

因为尚未在 HSQLDB 中创建模式。

添加:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.schema=classpath:test-schema.sql

wheretest-schema.sql只是CREATE SCHEMA IF NOT EXISTS schema;让我更进一步,但现在当测试运行时,它会尝试运行一些运行 sql 插入的 liquibase,就像INSERT INTO table没有附加架构一样,它失败了。不幸的是,这个变更集已经针对实际的应用程序运行,所以我无法真正改变它。

有没有办法在spring.jpa.properties.hibernate.globally_quoted_identifiers=true不摆脱这些变更集的情况下将模式设置为工作,或者测试只是完全被束缚了?有没有办法让 liquibase 运行 sql 语句并自动为它们使用默认模式?

如果有帮助,这就是我的 application-test.properties 的样子:

spring.datasource.url=jdbc:hsqldb:mem:testdb;
spring.datasource.driver-class-name=org.hsqldb.jdbcDriver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.liquibase.url=${spring.datasource.url}
spring.liquibase.change-log=classpath:db/changelog/db.changelog-test.xml
spring.liquibase.user=sa
spring.liquibase.contexts=test
spring.liquibase.password=

spring.liquibase.default-schema=schema
spring.jpa.hibernate.ddl-auto=update
spring.datasource.schema=classpath:test-schema.sql

标签: spring-bootliquibasehsqldb

解决方案


推荐阅读