How to re-create database before each test in Spring?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When it comes to software development, especially in testing environments, it is essential to ensure that each test runs in a freshly prepared environment. This avoids inter-test dependencies and ensures the reliability and predictability of test results. In the context of database testing with Spring, this often means re-creating or resetting the database before each test. Here's a comprehensive guide on how to achieve this in Spring applications.
Why Re-Create a Database Before Each Test?
- Isolation: Tests are isolated from each other, which helps in identifying specific issues rather than introducing test flakiness due to shared state.
- Repeatability: Allows for consistent test results every time they are run.
- Independence: Each test can function independently of any side effects caused by other tests.
- Clean Slate: Ensures that changes made in one test do not affect subsequent tests.
Approaches to Re-Create a Database in Spring
Several strategies can be employed to ensure that you have a clean database state before each test in a Spring application:
Using In-Memory Databases
For applications with relatively simple database setups or for unit testing purposes, using an in-memory database such as H2 or HSQLDB might be an ideal choice:
- Configure your Spring application to use an in-memory database profile during testing.
- Spring Boot makes it easy by auto-configuring an embedded database if available on the classpath.
Example Setup:
Using Flyway or Liquibase for Schema Initialization
For more complex scenarios, especially when the database schema and initial data setup need to be controlled, tools like Flyway or Liquibase are extremely useful:
- Use migration scripts to set up the database schema.
- Ensure these scripts run before each test.
Flyway Example:
- Create SQL migration scripts under
src/main/resources/db/migration. - Flyway will automatically execute these scripts on application startup.
Leveraging Spring's @DirtiesContext
Sometimes it's necessary to not only re-create the database but also refresh the entire application context to clear state:
- Use
@DirtiesContextto indicate that the application context should be cleaned up after the test class or method.
Example Usage:
Summary Table
Here’s a quick look at the key strategies and their considerations:
| Approach | Pros | Cons |
| In-Memory Database | Fast setup, great for unit tests | May not fully replicate production environments |
| Flyway/Liquibase | Works well for complex, production-like setups | Requires managing additional migration scripts |
@DirtiesContext | Ensures complete application context reset | Slower, as it redeploys the entire Spring context |
Best Practices
- Choose Your Approach Wisely: Depending on your database and testing requirements, choose between in-memory databases or database versioning tools.
- Maintain Test Hygiene: Use annotations judiciously to maintain efficient test execution.
- Keep Migration Scripts Up-to-Date: Regularly update and maintain your migration scripts to align with the latest database schema and data requirements.
Conclusion
Re-creating your database before each test in a Spring application can be crucial for ensuring test reliability, repeatability, and independence. By leveraging Spring’s capabilities along with tools like Flyway, Liquibase, and embedded databases, you create a robust testing environment that accurately reflects your application's data state. This not only improves test quality but also speeds up the development cycle by catching issues early in the testing phase.
Related reading
- How to re-sync the Mysql DB if Master and slave have different database incase of Mysql replication?
- How to read from specific instance of a documentdb cluster
- How to read SQL Table data into a C DataTable
- how to rebalance cassandra cluster after adding new node
- How to read a text-file resource into Java unit test?
- How to read data from java properties file using Spring Boot
- How to re-send external tester invitations on Apple's TestFlight service
- How to reset between tests

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.