Skip to content

How the Test Database Works

Tobias314 edited this page Sep 4, 2018 · 2 revisions

A Second Database Schema for Testing

It is important to know that Chimera uses a separate database schema for testing. So please create a second schema for testing in your database and specify its URL in the MySQL.test.url property of the properties.config file. Every time when maven run tests this schema will be deleted and recreated. Afterward, every database access in the tests will be done against this schema. So make sure that every data you need during your tests is written into the database (e.g. by overwriting the setup() method).

What Under the Hood Happens

This process is not relevant for development in the first place but becomes relevant when e.g. trying to understand and modify the maven build process of chimera or when problems with the test database occur.

First of all there exist 2 slightly different persistence files under src/main/resources/META-INF. These are persistence.xml for normal deployment and persistenceTest.xml for testing. The only difference between these two is, that persistenceTest.xml uses another database schema as well as drop-and-create-tables for DDL-generation (so all old data is deleted before all tests are started).

When now maven builds chimera the following happens.
First of all both persistence files are filtered and maven properties like ${mysql.username} are replaced by strings. Then during mavens test-compile phase the persistence.xml file is copied, renamed and saved as persistence.xml.proper. Now the persistenceTest.xml file is renamed to persistence.xml so that when the tests are run the used persistence.xml file is the previous persistenceTest.xml file and contains the testing database configurations. After testing during mavens prepare-package phase the previously saved persistence.proper.xml is copied and renamed back and now replaces the persistence.xml (which was used for testing). So finally the original persistence.xml file which contains the correct database schema for final deployment is in place and used for chimera.

All this becomes possible using mavens build phases and the copy-rename-maven-plugin. The section of these plugin in the pom.xml file is also the place where most of the process described above is configured. If you never have used maven before it could be helpful to get a very basic understanding of the maven build process and especially the role of mavens build phases as well as mavens ability to use plugins. Nevertheless, most of the statements in the pom.xml are self-explaining so it should be also understandable if you have never heard of maven before.