1

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.

Neil Stockton
  • 10,922
  • 3
  • 30
  • 28
Suryavanshi
  • 585
  • 5
  • 23

3 Answers3

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
  • 47,133
  • 13
  • 106
  • 128
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

Community
  • 1
  • 1
derkoe
  • 4,516
  • 2
  • 23
  • 28
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,416
  • 8
  • 43
  • 65