...
Drawio | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
- Docker provides a container with running database instance which is used during the test
- Test container component communicates with Docker via CLI (or Engine API depending on Docker deployment),
manages required container to be created/started
before test and stopped/removed after the test is completed - Test container component is used by JUnit test as a Class RuleRule
- The connection to test database is served using standard Spring framework componentscomponents
More details on implementation are below.
Test Containers
Running instance of database for integration test is provided by Test Java library component.
Docker availability is a prerequisite.
Test container life circle management
Test Containers library is integrated with JUnit via Rule interface implementation:
- On start() method invocation the requested test container is created (if absent) within a Docker and started (if not running yet)
- On stop() method invocation the test container is stopped and removed
If annotated as \@Rule the test container creation/start then stop/removal operations will be performed for each test method.
If annotated as \@ClassRule these operation will be performed for each test class.
In order to save the resources (and all tests execution time as result) the default TestContainer implementation was wrapped to custom
(singleton type of) artifact. This way the database test container is initialized only once for all the tests. The test container removal is
linked to JVM shutdown event to avoid this container remain running in docker.
Drawio | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
NB. The default testcontainer removal by default is served using dedicated RYUK container (part of TestContainers library). However
this container require to bu running as privileged, so it was disabled in order make TestContainers available on Jenkins.
Database initialization notes
@SpringBootTest
Test template
Resources
Frameworks and libraries:
Tutorials:
- https://reflectoring.io/spring-boot-data-jpa-test/
- https://reflectoring.io/spring-boot-test/
- https://www.baeldung.com/docker-test-containers
- https://www.baeldung.com/spring-boot-testcontainers-integration-test
...