Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The CPS-RI module which is responsible for data persistence and retrieval from the database is based on
Spring on Spring Data framework.
Following components are used:

...

In order to reach the desirable level of reliability it's expected the functionality to be covered with tests.

Below is comparison between unit test vs integration test approaches.

draw.io Diagram
borderfalse
diagramNameCPS DB Testing Unit vs Integration
simpleViewerfalse
linksauto
tbstyleinline
lboxfalse
diagramWidth851
revision7

...

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.

To be used the @SpringBootApplication annotated class is required. However both CPS Spring Boot application class and  and associated configuration are allocated
are allocated in CPS-REST maven module which is cannot be accessed from CPS-RI module on build (when the tests classes are compiled and executed).

In So in order to fulfill the @SpringBootTest requirements and to properly initialize DataSource object the substitutions for both Spring Boot application
class and configuration (DataSource properties) are set directly within CPS-RI module (test scope).

draw.io Diagram
borderfalse
diagramNameCPS Spring boot RI dependencies on build
simpleViewerfalse
width
linksauto
tbstyleinline
diagramDisplayName
lboxfalse
diagramWidth651
revision2

Database initialization notes

3

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

Code Block
languageyml
spring:
  datasource:
    url: ${DB_URL}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    driverClassName: org.postgresql.Driver
    initialization-mode: always

The database connection parameters are expected to be defined using environment variables.

Due to  It should be taken into account the ports exposed for a test containers are defined dynamically (part of DB_URL) are variable (to allow concurrent tests executions
and to avoid conflicts with containers already running within a Docker. It means the connection parameters for a newly created test container
require to be set before

...


existing containers) the expected variables are set after the test container directly from TestContainer wrapper artifact

Code Block
languagejava
titleDatabaseTestContainer.java
    @Override
    public void start() {
        super.start();
        System.setProperty("DB_URL", databaseTestContainer.getJdbcUrl());
        System.setProperty("DB_USERNAME", databaseTestContainer.getUsername());
        System.setProperty("DB_PASSWORD", databaseTestContainer.getPassword());
    }


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
languagejava
    @Test
	@Sql({"/data/clear.sql", "/data/test-data.sql"})
    public void test() {
		// test 
    }

See https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testcontext-executing-sql-declaratively


Resources

Frameworks and libraries:

Tutorials:

...