2

I would like to start a fresh postgres docker container before each single test execution, and NOT just once before running all integration tests. The configuration I currently have is like most Google results suggest:

     <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.33.0</version>
                <configuration>
                    <imagePullPolicy>IfNotPresent</imagePullPolicy>
                    <images>
                        <image>
                            <alias>it-database</alias>
                            <name>postgres:11.3</name>
                            <run>
                                <namingStrategy>alias</namingStrategy>
                                <ports>
                                    <port>5555:5432</port>
                                </ports>
                                <wait>
                                    <log>(?s)database system is ready to accept connections.*database system is ready to accept connections
                                    </log>
                                    <time>20000</time>
                                </wait>
                            </run>
                        </image>
                    </images>
                </configuration>
                <executions>
                    <execution>
                        <id>docker:start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>docker:stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Vy Do
  • 46,709
  • 59
  • 215
  • 313
micgn
  • 245
  • 1
  • 4
  • 14

1 Answers1

2

I strongly recommend to take a deep look into Testcontainers cause testcontainers can exactly do what you like within your integration test.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Thanks for the hint. At the moment my tests are running with an in-memory h2 database. I would prefer not having to change all tests - it seems Testcontainers require additional code for starting in each test. – micgn Sep 02 '20 at 06:35
  • Exactly a single line if you are on JUnit.4 `container rule` in JUnit 5 you could it in serveral ways... and the H2 can be done with Testcontainers as well..The question is what kind of application you are developing (Spring Boot?) ? – khmarbaise Sep 02 '20 at 09:01
  • yes, Spring Boot, > 30 micro services, many many tests. I can easily change the database connection string to postgres, but not all tests – micgn Sep 02 '20 at 09:10
  • Then it is more easier to use JUnit 5 if you not already do so. I strongly recommend to go that path and change your tests to use testcontainers makes in general such kind of tests easier. The question do you need the postgresql container for integration test or for unit tests as you already mentioned using H2 ... so I suppose that means not many of the tests using postgresql ... – khmarbaise Sep 02 '20 at 11:40
  • The approach with the docker-maven-plugin already works fine, only that I am not able to restart the docker container often enough. And this requires not any change on the Unit tests. Testcontainers are an interesting suggestion, but also a bit off topic given my question above. – micgn Sep 02 '20 at 12:39