lundi 24 décembre 2018

How to correctly set tests to run with H2 and alongside with MySql for the application in SpringBoot

I have implemented the spring boot application to work with MySql database and that is working perfectly fine. After that I started to implement some tests and I would like to use H2 db for the test purposes but I'm having issues with sql scripts when running tests.

In order to do that I added dependency

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
<scope>test</scope>
</dependency>

after that in the src/test/resources i have create application.yml with settings

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

  jpa:
    properties:
      hibernate:
        format_sql: true
        show_sql: true
        dialect: org.hibernate.dialect.H2Dialect
eureka:
  client:
    enabled: false

alongside with schema and data scripts for testing purposes.

Currently I have only main test

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {

  @Test
  public void contextLoads(){
  }

}

My problem is that when I run the tests I get an error "Syntax error in SQL statement" because it tried to execute scripts from src/main/resources. The errors are:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement

Caused by: org.springframework.beans.factory.BeanCreationException: ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@14028087]to prepare test instance java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #8 of URL [file:/.../my-service/target/classes/schema.sql]
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE ... " expected "USING, ,, )"; SQL statement:

The errors are reasonable, spring boot is trying to execute sql scripts that are not in correct syntax to be executed on H2 DB, is there any way to state that when running tests schemas from src/main/resources to be executed or there is something that is not correctly configured?

Aucun commentaire:

Enregistrer un commentaire