java - 使用H2方言进行测试时,Spring正在设置mysql方言

我在 Spring 进行了一些测试,但看到了一件奇怪的事。

当我像这样在application.properties中设置数据源时

spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

并使用 @DataJpaTest设置H2数据库进行测试, Spring 使用mysql方言代替H2方言。

这是几行控制台
2018-08-22 14:42:53.881  INFO 852 --- [           main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2018-08-22 14:42:53.882  INFO 852 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2018-08-22 14:42:54.066  INFO 852 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:7ee61de9-3134-4b5e-93cb-02b1c11727ee;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2018-08-22 14:42:54.410  INFO 852 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-08-22 14:42:54.426  INFO 852 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [  name: default   ...]
2018-08-22 14:42:54.479  INFO 852 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.17.Final}
2018-08-22 14:42:54.480  INFO 852 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-08-22 14:42:54.510  INFO 852 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-08-22 14:42:54.605  INFO 852 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect

我看到Spring的 H2EmbeddedDatabaseConfigurer.java没有设置H2数据库的方言。

测试通过没有任何错误,但是就像H2被自动置于mysql模式一样。

与derby和hsql数据库相同。有人可以解释为什么会这样吗?

最佳答案

我发现绕过此问题的唯一方法是在application.yml上创建/src/test/resources/并手动配置H2:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
    username: sa
    password: sa