恐怕如果你的脚本没有复制到类路径中,那么选项并不多。
请尝试以下方法:
@Sql("file:/path-to-mainFolder/mainFolder/scripts/postgres/script.sql")
正如您在documentation 中看到的,您可以使用任何有效的资源类型。
话虽如此,我认为最好的选择是让这些资源在类路径中可用。如果您使用的是 maven,您可以使用例如 maven resources plugin 的 copy-resources 目标在运行测试时复制您的资源:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need: validate, test-compile... -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/scripts</outputDirectory>
<resources>
<resource>
<!-- Depending on your project, try defining the scripts src location as you consider more appropriate -->
<directory>mainFolder/scripts/postgres/script.sql</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
通过此设置,现在您可以将脚本位置定义为类路径资源:
@Sql("/scripts/postgres/script.sql")
也许,我不确定,以类似的方式,您也可以使用插件testResources 目标。