0

We have MYSQL for production and i am doing ( well trying to ) add h2 for Testing the Database .... here there is the problem .

I am adding an application.properties under src/test/resources

enter image description here

and the default is as usually under src/java/resources

enter image description here

The problem is the way application.properties under src/java/resources is congifured to use MYSQL like :

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connectionTimeout=1000
spring.datasource.hikari.idleTimeout=60000
spring.datasource.hikari.minimumIdle=2
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.poolName=FmPool
spring.datasource.hikari.connectionTestQuery=SELECT 1

#-- The below three are causing problem to H2 configuration --
spring.datasource.hikari.autoCommit=false
spring.jpa.database-platform=com.intralot.l10.core.jpa.MySqlDialect
spring.jpa.hibernate.ddl-auto=none

while the application.properties under src/test/resources for h2 looks like below :

# H2 Console
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=admin
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
#spring.datasource.hikari.autoCommit=true
#spring.jpa.hibernate.ddl-auto=create-update

# JPA
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

# Flyway
spring.flyway.enabled=false

when i comment from application.properties under src/java/resource the below lines:

#-- The below three are causing problem to H2 configuration --
spring.datasource.hikari.autoCommit=false
spring.jpa.database-platform=com.intralot.l10.core.jpa.MySqlDialect
spring.jpa.hibernate.ddl-auto=none

the H2 configuration works well but i don't know why that happens ..

Is there any way i can completely ignore default application.properties under src/java/resources and use only the application.properties under src/test/resources ?

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 1
    Just a random hint from a random guy from the Internet: I also tried to create a test db with H2 (and DbUnit) and it worked, but it was no fun at all and hard to maintain. For another project I found the following post: https://stackoverflow.com/questions/48956743/embedded-postgres-for-spring-boot-tests . I tried that, and it worked better, because the production DB (in my case Postgres) is the same as the test db. So you do not have to care about differences in the SQL dialects. And in my case there were other advantages. Perhaps check if embedded MySQL servers exists? – gillesB Jan 16 '19 at 12:38
  • Are you 100% sure, that your tests are using test config? Have you specified your test config class with @TestConfiguration annotation? Have you explicitly registered your test config class in spring context (i.e. by using something like this @Import(MyTestConfiguration.class)) ? – Tinki Jan 16 '19 at 12:43
  • @Tinki I am very new to Spring generally can you post an answer based on your comment ... i will at least put +1 :) – GOXR3PLUS Jan 16 '19 at 12:57

0 Answers0