Reading.sql file from custom location other than resouces folder using @sql spring boot test

huangapple go评论69阅读模式
英文:

Reading.sql file from custom location other than resouces folder using @sql spring boot test

问题

代码部分不要翻译,只返回翻译好的内容:

你好,我已经使用 Spring Data JPA 测试编写了测试用例。当我将 data.sqlschema.sql 文件放入 test/resources 文件夹中时,测试用例可以正常运行,即使没有使用 @Sql 注解,这是由于 Spring Boot 测试的默认行为。

但是我的要求是,我有一个与 maintest 文件夹并列的文件夹,即 integrationTest 文件夹,我的 data-h2.sql 和 schema-h2.sql 文件位于其中。问题是,我无法使用 @Sql 注解读取这些 sql 文件。如何指定路径,以便我可以从任何给定的自定义位置读取 sql 文件。

以下是文件夹结构和代码供参考:

(图片链接已省略)

代码

@DataJpaTest
@Sql(scripts={"classpath:/integrationTest/schema-h2.sql", "classpath:/integrationTest/data-h2.sql"})
public class AbcRepositoryTest extends AbstractTestNGSpringContextTests {

}

错误

08:44:45.329 [Test worker] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 90079, SQLState: 90079
08:44:45.329 [Test worker] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Schema "TEST" not found; SQL statement:

注意:上述翻译中的路径 "classpath:/integrationTest/schema-h2.sql" 和 "classpath:/integrationTest/data-h2.sql" 是用于从类路径中加载资源文件的标准 Spring 路径。

英文:

Hi I have written test case using spring data jpa test . Test case are running fine when i put data.sql and schema.sql file inside test/resources folder even though without using @Sql annotation because of default behaviour of spring boot test.

But my requirement is that i have one folder which is parallel to main and test folder i.e integrationTest where my data-h2.sql and schema-h2.sql file is residing . Problem is that I am not able to read these sql file using @Sql Annotation. how to give the path so that i can read sql file from any given custom location

Below is the folder structure and code for reference

Reading.sql file from custom location other than resouces folder using @sql spring boot test

Code

@DataJpaTest
@Sql(scripts={"/integrationTest/schema-h2.sql", "/integrationTest/data-h2.sql"})
public class AbcRepositoryTest extends AbstractTestNGSpringContextTests {

}

Error

08:44:45.329 [Test worker] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 90079, SQLState: 90079
08:44:45.329 [Test worker] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Schema "TEST" not found; SQL statement:

答案1

得分: 3

以下是翻译好的部分:

发现了在苦苦挣扎了3到4个小时后针对我发布的问题找到的解决方案,我们需要使用SqlScriptsTestExecutionListener来从您选择的自定义位置读取@Sql脚本,并且使用DirtiesContext以便@Sql脚本针对您的每个测试用例运行。

@DataJpaTest
@TestExecutionListeners(listeners = { SqlScriptsTestExecutionListener.class })
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@Sql(scripts = {"file:src/integrationTest/resources/schema-h2.sql", "file:src/integrationTest/resources/data-h2.sql"})
public class AbcRepositoryTest extends AbstractTestNGSpringContextTests {
}

有用的链接有助于解决这个问题:

英文:

Found the solution for the question i have posted after struggling 3 to 4 hour
we need to use SqlScriptsTestExecutionListener in order to read @Sql Scripts from custom location of your choice and put DirtiesContext so that @Sql script run for each of your test case.

@DataJpaTest
@TestExecutionListeners(listeners = { SqlScriptsTestExecutionListener.class })
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@Sql(scripts = {"file:src/integrationTest/resources/schema-h2.sql","file:src/integrationTest/resources/data-h2.sql"})
public class AbcRepositoryTest extends AbstractTestNGSpringContextTests {
}

UsefulLink help to solve the issue

https://github.com/spring-projects/spring-framework/issues/18929

https://stackoverflow.com/questions/47775560/how-can-springs-test-annotation-sql-behave-like-beforeclass

https://stackoverflow.com/questions/32871817/using-annotation-sql-is-it-possible-to-execute-scripts-in-class-level-before-m

huangapple
  • 本文由 发表于 2020年7月24日 14:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63068405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定