如何在没有嵌入数据源配置的情况下在 spring 启动测试期间执行`schema.sql`? [英] How execute `schema.sql` during spring boot test without embeded datasource configuration?

查看:54
本文介绍了如何在没有嵌入数据源配置的情况下在 spring 启动测试期间执行`schema.sql`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个带有 h2 数据库的 spring boot 应用程序,用作主数据库.还有一个 resource/schema.sql 在启动时被 spring boot 加载.

There is spring boot application with h2 database which is used as primary database. Also there is a resource/schema.sql wich is loaded at startup by spring boot.

但是在使用 @SpringBootTest 进行集成测试时,spring boot 不会加载这个 schema.sql.相反,它需要在已有 h2 db 的情况下设置嵌入式数据库.

But during integration tests with @SpringBootTest spring boot does not load this schema.sql. Instead it requires to setup embeded database while there is h2 db already.

有没有办法在没有嵌入数据源配置的情况下执行schema.sql?并且只对所有测试执行一次(例如,使用 @Sql 为所有测试创建架构不是解决方案)?

Is there a way to execute schema.sql without embeded datasource configuration? And do it only once for all tests (e.g. using @Sql for schema creation for all test is not a solution)?

推荐答案

@Sql(scripts = "classpath:schema.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) 注释你的类或方法 您的 schema.sql 在资源文件夹中的位置.

Annotate your class or method with @Sql(scripts = "classpath:schema.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) where your schema.sql is on resources folder.

如果您的测试被标记为集成测试,您就需要这个.如果您正在运行单元测试或只是在启用默认单元测试的情况下编译,Spring Boot 将为您执行此操作.

You need this if your tests are marked as integration tests. Spring boot will do this for you if you are running a unit test or just compiling with unit tests enabled which is the default.

这篇关于如何在没有嵌入数据源配置的情况下在 spring 启动测试期间执行`schema.sql`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆