简体   繁体   English

使用Intellij或Maven生成SQLite数据库以进行单元测试?

[英]Generate SQLite Database with Intellij or Maven for Unit tests?

I have written some unit tests for a Java repository called CountryRepository.java 我已经为一个名为CountryRepository.java的Java存储库编写了一些单元测试。

These unit tests are configured to use a generated SQLite DB file in the test resources folder like so: 这些单元测试被配置为使用测试资源文件夹中的生成的SQLite DB文件,如下所示:

 /**
 * 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 档案位置

在此处输入图片说明

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. 由于我们不想将数据库文件提交到git,因此我必须手动生成test.db文件,以便单元测试能够正常工作。

eg 例如

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? 每次运行测试时,如何配置Maven和/或Intellij来生成此数据库文件,以使其可靠运行?

Do I need to write a shell script and call it through Maven? 我需要编写一个Shell脚本并通过Maven调用它吗?

Bonus points for how I can do the same for the main/resources/db file 我如何对main / resources / db文件执行相同操作的要点

Thanks 谢谢

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. 我相信生成一个在“ beforeClass”上创建的jUnit ClassRule会创建数据库并加载架构,而在“ afterClass”上删除这些文件将是最适合您的方法。 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. 要以编程方式生成DB文件,只需创建连接即可。 See the link here with some sample code: http://www.sqlitetutorial.net/sqlite-java/create-database/ 请参见此处的链接和一些示例代码: http : //www.sqlitetutorial.net/sqlite-java/create-database/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM