When testing Springboot development, sometimes, we require to have different properties to allow for testing to be made easier.

We could make use of the anotation @TestPropertySource and define a new one at Test class level.

@TestPropertySource(locations = "/custom-test.properties")

But sometimes, we might have to have some additional files under our resources to make use of them.

For instance, in springboot and for local testing, we might end up having an In-memory sql using H2 database to have some insertions previously to our tests, and we might want to have them separately from ou main/src/resources folder.

To set our test resources folder at project level, we could define them in our maven file.

Example on how to set properties and other at maven level, that will be picked when testing without having to define other properties at test level.

<build>
    ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    ...
</build>

This also allow us to define test resources with maven profiles, without explicitly setting test profiles with springboot.

Example

<profile>
<id>integration</id>
<build>
    ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/integration/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    ...
</build>
</profile>
<profile>
<id>unit</id>
<build>
    ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/unit/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    ...
</build>
</profile>

So now we are able to set our resources when running maven test

For example we could have a postgres-data.sql in our above defined integration profile, and a h2-data.sql defined under our resources folder for unit profile, as well as many other examples of resources used during test.

In order to use the above profiles we would simply do:

mvn test -P unit

Would run tests with resources for unit

mvn test -P integration

Would run with integration resources.

Thanks for reading, hope you found it helpful

André Ilhicas dos Santos

Devops Padawan, curious about systems automation, learning new languages, paradigms tools each day.

ilhicas ilhicas


Published