Mysql SpringBoot&x27;s带飞道的DataJpaTest

Mysql SpringBoot&x27;s带飞道的DataJpaTest,mysql,testing,spring-boot,spring-data-jpa,flyway,Mysql,Testing,Spring Boot,Spring Data Jpa,Flyway,我在项目中使用flyway来设置DB模式。当我尝试使用@DataJpaTest注释测试某个JPA层时,在启动时出现以下错误: > Caused by: > org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateSqlException: > Migration V1__init.sql failed > ----------------------------- SQL State : 42581 Error

我在项目中使用flyway来设置DB模式。当我尝试使用@DataJpaTest注释测试某个JPA层时,在启动时出现以下错误:

> 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,
看起来它正在尝试使用HSQL而不是常规SQL

有没有一种方法可以将@DataJpaTest与flyway一起使用?

它正试图使用它来执行脚本。您可以做的是为测试创建一个数据库模式,并在不同的概要文件中使用它。假设您使用的是MySQL,那么在资源源文件夹中可以有一个
应用程序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
激活此配置文件,并使用
@AutoConfigureTestDatabase
中的注释配置禁用HSQLDB:

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

我仍然得到相同的错误(flyway仍然试图用HSQLDB执行我的迁移脚本)是您的
应用程序测试。属性
内部
src/main/resources