SGiux
SGiux

Reputation: 829

@Sql annotation by passing a sql script present in another module

I have a project compose by more than one module, and an integration test (in the test folder) where I want to run this script using the @sql annotation. By default the class path resource is used.

The test is inside this folder:

mainFolder/module1/src/test/java/com/.../.../controllers/TestClass.java

while the script is present in this folder:

mainFolder/scripts/postgres/script.sql

Basically I'm not sure which string (relative path) I should put in the value parameter of the @Sql annotation.

Upvotes: 2

Views: 1030

Answers (2)

Narcis Postolache
Narcis Postolache

Reputation: 229

If you want to use the scripts on your local environment machine you can also add the directory mainFolder/scripts/postgres as a dependency. In Intellij, you con go to Project Structure -> Modules -> your module -> dependencies -> + -> Jars or directories -> your script directory -> Classes. Then you will be able to use

@Sql("/yourScript.sql")

In DevOps pipeline you would need to add the dedicated directory to your application server classpath.

Keep in mind that you will have to keep unique names for your sql files, because if you intend to use a structure with multiple directory levels, I think the jvm will load only the first entry. It is not a recommended approach, but it is fast if you only need to generate a report or test something on your local environment.

Another approach would be to specify at runtime -Xbootclasspath/a:. Then you would be able to use @Sql with relative paths as you initially wanted. E.g.

-Xbootclasspath/a:path-to-mainFolder

then you would be able to use

@Sql("/scripts/postgres/script.sql")

Upvotes: 0

jccampanero
jccampanero

Reputation: 53411

I am afraid that if your scripts are not copied to the class path there are not a lot of options.

Please, try something like:

@Sql("file:/path-to-mainFolder/mainFolder/scripts/postgres/script.sql")

As you can see in the documentation you can use any valid resource type.

Having said that, I think the best option is to make these resources available in the classpath. If you are using maven, you can use for instance the copy-resources goal of the maven resources plugin to copy your resources when running your tests:

<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>

With this setup, now you can define your script location as a classpath resource:

@Sql("/scripts/postgres/script.sql")

Perhaps, I am not sure about that, in a similar fashion you can use the plugin testResources goal as well.

Upvotes: 1

Related Questions