Table Of Contents
Table of Contents |
---|
AddressesRelated Jiras:
Jira Legacy server System Jira serverId 4733707d-2057-3a0f-ae5e-4fd8aff50176 key CPS-95 Jira Legacy server System Jira serverId 4733707d-2057-3a0f-ae5e-4fd8aff50176 key CPS-124 Jira Legacy server System Jira serverId 4733707d-2057-3a0f-ae5e-4fd8aff50176 key CPS-128
Overview
The CPS-RI module which is responsible for data persistence and retrieval from the database is based on Spring Data framework.
Following components are used:
...
- All levels of abstraction are involved in tests, it means all of them are impacting on test results same way as within
a deployed application - JPA repositories are subjects of test
- Complete validation of expected behavior, including database schema initialization
- Can be used on build, keeping desired level of reliability and consistency
- Tests can be used on development stage making the database deployment (on dev environment) optional
Components
Components used to serve the integration tests are shown on diagram below.
...
More details on implementation are below.
Test Containers
Running instance of database for integration test is provided by TestContainers Java library component.
Docker availability is a prerequisite.
Test container life circle management
Test Containers library is integrated with JUnit via Rule interface implementation:
...
NB. The default remaining test containers removal (on JVM termination) was served by dedicated RYUK container (part of TestContainers library).
However this container requires running as privileged one, so it was disabled in order make TestContainers available on Jenkins. It made it
necessary to explicitly invoke test container removal logic on JVM termination event.
@SpringBootTest
Using @SpringBootTest
annotation for tests allows @Autowired
Spring components (including JPA repository instances) initialization for testing
same way as it's done on regular Spring Boot application execution.
...
NB. There is no need to initialize the Spring Boot application within CPS-RI in same scale as it's done for CPS-REST module, so the
application.yaml configuration within CPS-RI module contains mainly database related parameters. In case these parameters are updated
within primary configuration file same changes require to be provided to test configuration file as well.
Database initialization
The datasource is configured in application.yml test configuration file like below
...
The database schema initialization is performed by Spring framework (same way for both runtime and testing) using schema.sql file.
See https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-database-initialization
Test data
Spring framework allows data setting for testing using @Sql
annotation.
Single or multiple SQL scripts can be defined to be executed before the test method (resources folder is a root).
Code Block | ||
---|---|---|
| ||
@Test @Sql({"/data/clear.sql", "/data/test-data.sql"}) public void test() { // test } |
Test class template
Below is an example of test class (setup ) which can be used as a template for new database tests being added to CPS project.
...
language | java |
---|
...
Note. During 2023 CPS moved away from using SQL scripts to insert Test Data. Instead we insert dat programmatically during the test for more details see CPS (semi-) Integration Tests
Resources
Frameworks and libraries:
...