I am creating a web app using Spring-boot
and Jpa
. Everything is working fine. But during testing I have to manually create the obejct instances and populate the database for each test. I was wondering if I can backup the embedded db and restore it at the beginning of each test, freeing up the clutter in my test code.
Asked
Active
Viewed 2,009 times
1

Neil Stockton
- 11,383
- 3
- 34
- 29

Suryavanshi
- 595
- 1
- 7
- 24
3 Answers
1
H2 allows you to create a SQL script using script to
. Then you can run a SQL script when opening a connection by appending init=...
to the database URL.

Thomas Mueller
- 48,905
- 14
- 116
- 132
0
The best (and easiest way) is to use Spring embedded database + SQL scripts to preload your database. A simple way to do this is described in this answer.
A very good article showing the population per test (and the whole setup) is: Setup and preload database for spring integration/functional tests
0
Spring Boot has several mechanisms for initialising a database, the simplest one is arguably via Spring JDBC. All you need to do is to create a file called data-test.sql
(assuming your your test profile is named "test") and define all the data via plain SQL inserts in there.

kryger
- 12,906
- 8
- 44
- 65