Search code examples
testingspring-data-jpasql-scripts

Sql scripts for creation of same table in repository tests


I have several Spring data repositories which have some joins to the same table, lets say, Client table. So in order to not repeat the sql script for the creation of the Client table for each repository test (it would be a bad practice), would it be a good practice to externalize the creation of this table in a single sql file and import it in the tests they need it?

Thanks!


Solution

  • The normal practice is not to create a custom schema for each test, but create the full schema for all tests using the same script/mechanism that you'll use in production.

    Database schema aren't modular, they have dependencies all over the place. These dependencies come in the form of foreign keys, but also as triggers or materialised views and constraints.

    If your schema is small and simple there is no point in going through the hassle of breaking it into pieces. If it is large it becomes easy to miss something and have tests that don't properly represent the production environment.