Yuri Adeodato
Yuri Adeodato

Reputation: 41

Spring is setting mysql dialect when using h2 dialect for test

I'm doing some tests in Spring and I saw a strange thing.

When I set datasource in application.properties like this

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

and use @DataJpaTest for setting H2 database for testings, the spring uses mysql dialect instead H2 dialect.

Here is some lines of console

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

I saw that H2EmbeddedDatabaseConfigurer.java of Spring does not set the dialect for H2 database.

The test pass with no errors, but it's like that H2 was putted in mysql mode automatically.

Is the same with derby and hsql databases. Someone can explain why this happen?

Upvotes: 6

Views: 3241

Answers (1)

Dherik
Dherik

Reputation: 19070

The only way that I found to bypass this problem was create the application.yml on the /src/test/resources/ and configure the H2 manually:

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

Upvotes: 1

Related Questions