Suryavanshi
Suryavanshi

Reputation: 605

Backup embedded database (H2)

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.

Upvotes: 1

Views: 2193

Answers (3)

kryger
kryger

Reputation: 13181

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 test profile is named "test") and define all the data via plain SQL inserts in there.

Upvotes: 0

derkoe
derkoe

Reputation: 6271

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

Upvotes: 0

Thomas Mueller
Thomas Mueller

Reputation: 50087

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.

Upvotes: 1

Related Questions