我有一个使用H2内存数据库进行集成测试的SpringBoot应用程序。如果我在versino 2.3.4.RELEASE中使用SpringBoot,则测试有效。如果我升级到2.4.0并出现以下错误,它们将失败:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver com.microsoft.sqlserver.jdbc.SQLServerDriver claims to not accept jdbcUrl, jdbc:h2:mem:integrationTestDB;DB_CLOSE_DELAY=-1;MODE=MSSQLServer;INIT=CREATE SCHEMA IF NOT EXISTS dbo\;SET SCHEMA dbo
Caused by: java.lang.RuntimeException: Driver com.microsoft.sqlserver.jdbc.SQLServerDriver claims to not accept jdbcUrl, jdbc:h2:mem:integrationTestDB;DB_CLOSE_DELAY=-1;MODE=MSSQLServer;INIT=CREATE SCHEMA IF NOT EXISTS dbo\;SET SCHEMA dbo
这是测试所使用的我的integration-test.properties:
spring.datasource.url=jdbc:h2:mem:integrationTestDB;\
DB_CLOSE_DELAY=-1;\
MODE=MSSQLServer;\
INIT=CREATE SCHEMA IF NOT EXISTS dbo\\;SET SCHEMA dbo
spring.datasource.driver=org.h2.Driver
spring.datasource.hikari.driver-class-name=org.h2.Driver
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.hbm2ddl.auto=none
spring.datasource.username=sa
spring.datasource.password=
spring.liquibase.user=sa
spring.liquibase.password=
H2的版本是1.4.200。成功和失败之间的区别是pom父元素中的SpringBoot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
liquibase版本从3.8.9更改为3.10.3。我将其配置为保持在3.8.9,但这没有帮助。
我阅读了发行说明,并找到了有关嵌入式数据库检测的部分:https : //github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4-Release-Notes#embedded-database-detection
但是添加
spring.datasource.initialization-mode=always
到属性也没有帮助。
我记得上次花了一些时间才能找到正确的数据源网址,但是我找不到与Google相关的新线索。
您能给我一个提示,是什么原因导致这个问题?
亲切的问候,乌尔里希
您的属性包含H2和SQLServer配置的混合。例如,您已经为数据源配置了H2 JDBC URL,但已将Hibernate配置为使用SQLServerDialect
。异常表明,尝试初始化Liquibase时正在使用SQLServer的JDBC驱动程序。在我看来,您似乎正在尝试在集成测试中使用H2,取代部署应用程序时使用的SQLServer。
2.4.0中有一个新spring.liquibase.driver-class-name
属性。如果未设置,则退回到使用spring.datasource.driver-class-name
。没有spring.datasource.driver
属性,因此请尝试替换以下两行:
spring.datasource.driver=org.h2.Driver
spring.datasource.hikari.driver-class-name=org.h2.Driver
与以下行:
spring.datasource.driver-class-name=org.h2.Driver
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句