Home Springboot project using different resource folders for testing
Post
Cancel

Springboot project using different resource folders for testing

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<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

This post is licensed under CC BY 4.0 by the author.