当前位置: 首页 > 知识库问答 >
问题:

使用flyway测试扩展名创建名为“flywayInitializer”的bean时出错

王凯旋
2023-03-14

所以我有两个集成测试类。我正在使用flyway测试扩展重置db。当我从IntelliJ单独运行测试类时,两个都通过了。然而,当我使用mvn clean install或在IntelliJ all test中一起运行它们时,TestClass2会出现异常并失败。

创建名为“flywayInitializer”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]中定义:调用init方法失败;嵌套异常为org。flywaydb。果心应用程序编程接口。FlywayException:找到非空架构“public”,没有架构历史记录表!使用baseline()或将baselineOnMigrate设置为true来初始化架构历史记录表。

我试着设置Spring。飞行路线。在核心测试中,迁移基线=true。proberties文件,但没有成功。

TestClass1和TestClass2的区别在于,FlywayTest在类级和方法级各有一次。同样在TestClass1中,我对我的一些服务使用了MockBean。这是否会导致错误?

以下是我的测试:

@SpringBootTest(classes = { CoreTestConfig.class })
@TestPropertySource("classpath:core_test.properties")
@ExtendWith(SpringExtension.class)
@ExtendWith({FlywayTestExtension.class})
@FlywayTest // on class level
class TestClass1 {
    // contains injected mocks @MockBean
}
@SpringBootTest(classes = { CoreTestConfig.class })
@TestPropertySource("classpath:core_test.properties")
@ExtendWith(SpringExtension.class)
@ExtendWith({FlywayTestExtension.class})
class TestClass2 {

    @Test
    @FlywayTest // on method level
    void someTestMethod() {
        // ...
    }

}

测试的上下文配置如下:

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties({
        AppProperties.class
})
@EnableTransactionManagement
@EnableAsync
@EnableScheduling
@Import({ActivitiConfiguration.class})
@ComponentScan(basePackages = { "com.company.product.core" },
        excludeFilters = @ComponentScan.Filter(value = Configuration.class)
)
@EntityScan(basePackageClasses = BaseDO.class)
@EnableJpaRepositories(basePackages = "com.company.product.core.repository")
public class CoreTestConfig {
    // contains some specific test beans
}

我使用组织。flywaydb。flyway测试扩展版版本6.4.0。

更新:当我在TestClass1中删除@MockBean时,一起运行所有测试时不会再出现错误。但是我应该如何将@MockBean与flyway测试扩展一起使用?

共有1个答案

令狐高洁
2023-03-14

如本教程所示,模拟CoreTestConfig类中的bean解决了问题。我不确定为什么,但它有效。然后简单地为测试中的模拟服务@Autowire

 类似资料:
  • 我有一个实体类InAppNotification。看起来像这样的java: 我使用JPA来处理数据库查询,这就是JPA接口的定义: 这是我application.properties的样子: 但是,当我试图在构建后通过运行 来打包应用程序时,我会遇到以下问题: 尝试调用不存在的方法。尝试从以下位置进行:javax.el.ELManager.getExpress sionWorks(ELManage

  • 在将project从Spring Boot版本从1.2.3.release迁移到1.3.0.release之后,我已经开始得到以下异常。 创建类路径资源[org/springframework/boot/autoconfigure/admin/springapplicationadminjmxautoconfiguration.class]中定义的名为'Spring ApplicationAdmi

  • 我遵循本教程将消息发送到azure服务队列:https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-service-bus 到我现有的spring boot应用程序,但我得到以下错误: 用户类别: 控制器类: pom xml: 添加它

  • 我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*创建名为“client”的bean时出错*异常,不确定是什么地方出了问题,因为我之前找不到任何类似的线索...非常感谢你为大家指路。这是mu elasticsearch配置: 这是我的主要应用程序: 我的pom.xml: 这是te异常跟踪:

  • 我有这些错误编译,我不知道我哪里错了,我只有这3个类

  • 我正在尝试在spring boot应用程序中使用SQLite。但是应用程序不能创建下面的bean。 org.springframework.boot.autocigure.orm.jpa.hibernatejaconfiguration 我该怎么办?我在这个站点上搜索了相关的问题,但是找不到一个合适的。 如下所示。 4.0.0 org.springframework.Boot spring-boo