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.

3 Answers 3

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.