【问题标题】:Spring boot test H2 sql script not being loaded未加载 Spring Boot 测试 H2 sql 脚本
【发布时间】:2021-08-25 22:58:50
【问题描述】:

所以我在尝试为模式和数据加载 .sql 脚本时遇到问题。

默认情况下我的脚本没有被加载,因为当我的测试尝试从 H2 db 获取数据时,显示消息“表”未找到”。

脚本位于“src/test/resources”文件夹中。 pom中的H2依赖:

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

当我添加注释“@Sql(scripts = { "/schema.sql", "/data.sql" }) 时,我看到下一个错误:

java.lang.IllegalStateException:无法执行 SQL 脚本 测试上下文 [DefaultTestContext@7090eaec testClass = 微服务控制器测试,测试实例 = sche.invm.backend.rke.asn2019.get.controller.MicroserviceControllerTest@180274b1, testMethod = testGetPerson@MicroserviceControllerTest, testException = [空],合并上下文配置 = [WebMergedContextConfiguration@5158e446 testClass = MicroserviceControllerTest,位置 = '{}',类 = '{class sche.invm.backend.rke.asn2019.get.GetApplication}', contextInitializerClasses = '[]',activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = 设置[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@30bbcf91, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@52b959df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@31834a2b, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@34c53688, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@6127a7e], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', 父 = [null]],属性 = 地图['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]]: 无法从事务管理器 [org.springframework.orm.jpa.JpaTransactionManager] 获取数据源(命名为 '')。

这是我测试的配置:

这是我的带有@Sql注解的测试配置:

¿ 可能是什么错误? 我的应用配置了多租户,我不知道这是否会导致此特定错误。

【问题讨论】:

    标签: java integration-testing h2 spring-boot-test


    【解决方案1】:

    最后的问题是,在我的服务的应用程序中,属性“exclude = {DataSourceAutoConfiguration.class}”在@SpringBootApplication() 中被声明,删除它后我的测试运行没有问题。

    还有一点是因为我的服务是多租户的,所以我必须在我的属性文件中配置,除了我的多租户的url,还有spring.datasource的url(这是因为我的多租户url是以下jdbc:h2 :mem:testdb,但db h2以这种方式设置url jdbc:h2:mem:718e98df-3d08-46de-99e3-6ec4dc725293,所以脚本在这个db中执行而另一个db没有这些表)。

    我希望它被理解(我的英语不是最好的)

    这是我的测试属性文件的一部分:

    spring.datasource.url:jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    spring.datasource.username= sa
    spring.datasource.password= 
    
    #multitenant datasources
    datasources.0.tenant=CL
    datasources.0.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    datasources.0.username=sa
    datasources.0.password=
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-16
      • 2019-04-13
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 2017-02-03
      相关资源
      最近更新 更多