Within my last post the Camunda Spring-Boot Starter application used a h2 in-memory database. In general this is a good approach while developing and testing, but it should be replaced by a persistent database in later development and production stages. Using Spring-Boot capabilities this can be easily achieved by configuration. Within this post we will create a Camunda Spring-Boot starter database configuration which uses h2 in-memory database during automated tests only. For all other scenarios a MySQL database is used.
h2 Test Database Configuration
To begin with, the h2 dependency’s scope has to be changed within the project POM file. Up till now no explicit dependency scope was defined. Therefore, Maven uses default compile scope which adds this dependency to all project classpaths. In our case we want to change this behaviour such that h2 dependency is only available during testing. For this requirement the test scope can be used.
As result h2 database will only be available during Maven’s test phase. In order to make sure our application uses it when tests are executed, one has to provide an additional test application.properties file. Following Spring-Boot conventions, this file can be placed under src/test/resources/application.properties and is used when executing automated tests. Within this file test specific configuration can be stored which override standard configuration. In our case we need to specify h2 as database used by Camunda:
Now h2 database is only available during tests. Next we will add MySQL as database dependency for other development stages and production.
MySQL Database Configuration
Enabling our application to connect to Camunda BPM Platform to a MySQL database during startup is quite simple. First, MySQL Connector dependency must be added to the project’s POM file.
Second, we need to configure our application such that it uses MySQL as database and provide connection details. Within my development setup, I wanted to use a MySQL database during manual tests. Thanks to MySQL’s official Docker image it is quite easy to obtain a running MySQL instance on my local machine. To connect to it with our application, it just needs some additional configuration:
- MYSQL_ROOT_PASSWORD – Administration user password
- MYSQL_DATABASE – Database name which should be created on startup
- MYSQL_USER – User for created database
- MYSQL_PASSWORD – Password for created database
Taking this configuration into account, our application’s src/main/resources/application.properties file has to be extended using following properties:
# Database configuration
This already concludes the setup. Using this configuration an h2 database is started during automated tests. In contrast, Camunda BPM Platform connects to the MySQL database specified when starting the application in other development stages. Currently, this simple setup is sufficient for my requirements. At a later stage one might add a more sophisticated configuration using Spring-Boot Profiles.