Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the mode=mysql to embedded H2 DB in Spring Boot 1.4.1 for @DataJpaTest?

Tags:

I have some problems with using a schema.sql file to create my sql schema when executing a junit test while this schema contains mysql specific expression. I have to add the mode=mysql to the H2 url.

For example something like this: jdbc:h2:mem:testd;MODE=MYSQL

But Spring boot automatically uses the url defined in the enum org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection with its url

jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE.

I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL from my test-application.properties. All other settings from my test-application.properties have been read successfully.

If I let spring/hibernate create the schema (without the schema.sql file) with the javax.persistence annotations in my entities everything works fine.

Is there a simple way to add a mode?

like image 848
Marco Avatar asked Feb 21 '17 10:02

Marco


2 Answers

Set

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL 

in application-test.properties, plus

@RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @ActiveProfiles("test") 

on the test class

like image 116
Evgeny Batalov Avatar answered Oct 04 '22 17:10

Evgeny Batalov


I was having this same issue. It would not pick up the url when running tests. I'm using flyway to manage my scripts. I was able to get all of these working together by following these few steps.

Created a V1_init.sql script in src/test/resources/db/migration so that it is the first script run by flyway.

SET MODE MYSQL; /* another h2 way to set mode */  CREATE SCHEMA IF NOT EXISTS "public"; /* required due to issue with flyway --> https://stackoverflow.com/a/19115417/1224584*/ 

Updated application-test.yaml to include the schema name public:

flyway:   schemas: public 

Ensure the test specified the profile: @ActiveProfiles("test")

like image 43
anztenney Avatar answered Oct 04 '22 16:10

anztenney