Flyway Spring Boot单元测试失败-IllegalState无法加载ApplicationContext

时间:2020-01-31 06:03:59

标签: java hibernate spring-boot junit flyway

我有一个试图与Flyway集成的Spring Boot应用程序。我的应用程序启动正常,并在启动后在本地数据库上成功执行了迁移,但是我的JUnit测试失败,并显示消息IllegalState Failed to load ApplicationContext

我的应用bean的创建如下:

  @Bean(initMethod = "migrate")
  @Profile(value = {"default", "memory"})
  public Flyway flyway(DataSourceProperties dataSourceProperties) {
    Flyway flyway =
        Flyway.configure()
            .dataSource(
                dataSourceProperties.getUrl(),
                dataSourceProperties.getUsername(),
                dataSourceProperties.getPassword())
            .locations("classpath:db/migrations")
            .load();
    return flyway;
  }

我的测试配置bean如下:

  @Bean(initMethod = "migrate")
  @Profile("memory")
  public Flyway flyway(DataSourceProperties dataSourceProperties) {
    Flyway flyway =
        Flyway.configure()
            .dataSource(dataSourceProperties.getUrl(),
                        dataSourceProperties.getUsername(),
                        dataSourceProperties.getPassword())
            .locations("classpath:db/migration")
            .load();
    return flyway;
  }

我测试的flyway / JPA / spring属性如下:

spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=some_username
spring.datasource.password=some_password

# ===============================
# JPA / HIBERNATE / FLYWAY
# ===============================

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.flyway.enabled=false

我在测试配置中将飞车启用为false的原因是,我不想在飞车迁移上运行测试。我只想在我的H2的内存数据库中的H2上运行它们,该数据库是在测试中由休眠模式创建/管理的。 当我运行mvn test时,我的所有测试均以相同的错误IllegalState Failed to load ApplicationContext开始失败。

任何人都知道我在Bean创建/测试应用程序上下文中可能缺少什么吗?

编辑:错误/堆栈跟踪



[ERROR] testGetListLang(com.service.myservice.GetListLangTest)  Time elapsed: 0 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in com.service.myservice.AppConfig: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "PUBLIC" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "PUBLIC" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

0 个答案:

没有答案