2017-08-01 93 views
1

我在我的项目中使用flyway来设置数据库模式。当我尝试使用@DataJpaTest注释测试一些JPA层我在启动时出现此错误:SpringBoot与Flyway的DataJpaTest

> Caused by: 
> org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateSqlException: 
> Migration V1__init.sql failed 
> ----------------------------- SQL State : 42581 Error Code : -5581 Message : unexpected token: AUTO_INCREMENT : line: 2 Location : 
> db/migration/V1__init.sql 
> ..../target/classes/db/migration/V1__init.sql) Line  : 1 
> Statement : CREATE TABLE mytable ( id INT NOT NULL AUTO_INCREMENT 
> PRIMARY KEY, 

它看起来像它试图使用,而不是常规的SQL

HSQL有没有一种方法,我可以使用@DataJpaTest with flyway?

回答

1

它试图使用HSQLDB来执行脚本。你可以做的是为测试创建一个数据库模式,并在不同的配置文件中使用它。假设你使用MySQL,你可以有你的资源源文件夹这样内部的application-test.properties

spring.datasource.url=jdbc:mysql://localhost/test_db 
spring.datasource.username=your_user 
spring.datasource.password=your_pass 
spring.jpa.hibernate.ddl-auto=create 

,然后在您的测试类,你需要激活此配置文件与注释@ActiveProfiles和禁用HSQLDB与注释配置在@AutoConfigureTestDatabase

@DataJpaTest 
@ActiveProfiles("test") 
@AutoConfigureTestDatabase(replace = Replace.NONE) 
public class DBTest { ... } 
+0

我仍然得到同样的错误(飞路仍然试图用HSQLDB来执行我迁移脚本) – Johny19

+0

是'的src/main/resources'里面你'应用test.properties'? –

+0

检查我的更新 –