首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >春季引导测试H2 sql脚本未加载

春季引导测试H2 sql脚本未加载
EN

Stack Overflow用户
提问于 2021-06-08 23:18:55
回答 1查看 1.7K关注 0票数 3

因此,我在尝试加载模式和数据的.sql脚本时遇到了问题。

默认情况下,我的脚本没有加载,因为当我的测试试图从H2 db获取数据时,显示消息"Table "“not”。

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

代码语言:javascript
复制
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

当我将注释“@Sql”(脚本={ "/schema.sql","/data.sql“})放入时,我会看到下一个错误:

未能为测试上下文执行SQL脚本[DefaultTestContext@7090eaec testClass = MicroserviceControllerTest,testInstance = testGetPerson@MicroserviceControllerTest,testException = null,mergedContextConfiguration = [WebMergedContextConfiguration@5158e446 testClass = MicroserviceControllerTest,locations = '{}',class= '{class sche.invm.backend.rke.asn2019.get.GetApplication}',contextInitializerClasses =[]‘,activeProfiles = '{ test }',propertySourceLocations = '{}',propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true,setorg.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,}‘,contextCustomizers ='org.springframework.boot.test.context.SpringBootContextLoader',= 'src/main/webapp',contextLoader = map'org.springframework.test.context.web.ServletTestExecutionListener.activateListener‘resourceBasePath =null,resourceBasePath=map’org.springframework.test.context.web.ServletTestExecutionListener.activateListener‘-> false]:无法从事务管理器org.springframework.orm.jpa.JpaTransactionManager获得DataSource。

这是我的测试的配置:

这是使用@Sql注释进行测试的配置:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-09 23:15:21

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

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

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

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

代码语言:javascript
复制
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=
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67895671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档