我已经为名为 CountryRepository.java
这些单元测试配置为使用测试资源文件夹中生成的 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";
文件位置
由于我们不想将 DB 文件提交到 git,因此我必须手动生成 test.db 文件以便单元测试工作。
例如
sqlite3 test.db
> .read schema.sql
> .read test-seed.sql
如何配置 Maven 和/或 Intellij 在每次运行测试时生成此数据库文件,以便它可靠地工作?
我需要编写一个shell脚本并通过Maven调用它吗?
关于如何对 main/resources/db 文件执行相同操作的奖励点
谢谢
最佳答案
我相信生成一个 jUnit ClassRule,在“beforeClass”上创建数据库并加载架构,在“afterClass”上删除这些文件将是最适合您的方法。这样,仅为测试创建文件,您甚至可以在临时位置生成它们。 要以编程方式生成数据库文件,只需创建连接即可。请参阅此处的链接和一些示例代码: http://www.sqlitetutorial.net/sqlite-java/create-database/
关于java - 使用 Intellij 或 Maven 生成 SQLite 数据库进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505816/