tomaytotomato
tomaytotomato

Reputation: 4028

Generate SQLite Database with Intellij or Maven for Unit tests?

I have written some unit tests for a Java repository called CountryRepository.java

These unit tests are configured to use a generated SQLite DB file in the test resources folder like so:

 /**
 * This test relies on the test-seed.sql file located in /src/test/java/resources/db/country
 */
public class SqliteCountryRepositoryTest {

    private final static String DB_CONNECTION = "jdbc:sqlite::resource:db/country/test.db";

File location

enter image description here

Since we don't want to commit DB files to git, I have to manually generate the test.db file so that the unit tests work.

e.g.

sqlite3 test.db
> .read schema.sql
> .read test-seed.sql

How can I configure Maven and/or Intellij to generate this DB file each time I run the tests, so that it will reliably work?

Do I need to write a shell script and call it through Maven?

Bonus points for how I can do the same for the main/resources/db file

Thanks

Upvotes: 0

Views: 1596

Answers (1)

Boschi
Boschi

Reputation: 630

I believe generating a jUnit ClassRule that on "beforeClass" creates the DB and loads the schema, and on "afterClass" deletes those files would be the best approach for you. This way the files get created only for the test, and you even can generate them on a temp location. To generate the DB file programatically, it is just a matter of creating a connection. See the link here with some sample code: http://www.sqlitetutorial.net/sqlite-java/create-database/

Upvotes: 1

Related Questions