创建跨 Maven 测试阶段工作的临时数据库? [英] Creating temporary database that works across maven test phases?

查看:19
本文介绍了创建跨 Maven 测试阶段工作的临时数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加入了一个项目,该项目包含大量带有 SQL 语句的文件,用于创建用于集成测试的数据库.

I've joined a project that has a lot of files with SQL statements for creating a database that is used for integration testing.

我想知道如何使用这些文件来创建用于单元测试的数据库(使用 java 和 maven).

I'm wondering how I can use these files to create a database for unit testing (using java and maven).

我可以为每个单元测试创​​建一个 HSQL 内存数据库,甚至可以使用 spring jdbc 嵌入式数据库功能,但是在测试设置中要执行的 SQL 语句太多,因此无法扩展.

I can create a HSQL in-memory database for each unit test, or even use the spring jdbc embedded-database feature, but there's so many SQL statements to execute in the test setup that this is not scalable.

所以我想在 maven 测试阶段开始时创建一个临时数据库(加载 SQL 语句),让单元测试访问这个临时数据库并执行各种操作,然后在最后删除临时数据库Maven 测试阶段.

So I'd like to create a temporary database (that loads the SQL statements) at the start of the maven test phase, have the unit tests access this temporary database and perform various operations, then delete the temporary database at the end of the maven test phase.

我查看了 sql-maven-plugin,它允许我执行测试阶段,但我不确定如何配置一个可在所有单元测试中使用的临时数据库.没有要连接的服务器,并且内存数据库无法在多个单元测试中工作(我假设).

I've looked at sql-maven-plugin which would allow me to do the test phase executions, but I'm not sure how to configure a temporary database that will be available across all unit tests. There's no server to connect to, and in-memory database will not work across multiple unit tests (I assume).

一种选择是使用唯一的临时文件,例如将 JDBC 驱动程序 URL 指定为 jdbc:hsqldb:file:/path/to/temporary/file,但我不确定如何在 maven 中生成唯一的临时文件.

One option could be to use a unique temporary file, e.g. specifying the JDBC driver URL as jdbc:hsqldb:file:/path/to/temporary/file, but I'm not sure how to generate a unique temporary file in maven.

关于如何做到这一点的任何建议,或者是否有更好的方法可以采取?

Any suggestions on how to do this, or if there's a better approach to take?

更新:我决定使用在 target/db 目录中创建的基于文件的数据库.我使用 maven clean 插件在运行测试之前删除 target/db 目录,并使用 maven sql 插件从脚本创建数据库.

Update: I decide to use a file-based database created in target/db directory. I use the maven clean plugin to remove the target/db directory before tests are run, and the maven sql plugin to create the database from scripts.

推荐答案

对于这种情况,我创建了 derby-maven-插件.它可从 Maven Central 获得,因此您无需添加任何额外的存储库或任何内容.

For this case I have created the derby-maven-plugin. It's available from Maven Central, so you don't need to add any extra repositories or anything.

你可以这样使用它:

    <project ...>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.carlspring.maven</groupId>
                    <artifactId>derby-maven-plugin</artifactId>
                    <version>1.8</version>
                    <configuration>
                        <basedir>${project.build.directory}/derby</basedir>
                        <port>1527</port>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-derby</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>stop-derby</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

有关更多信息,您还可以查看USAGE.

For more info you can also check the USAGE.

这篇关于创建跨 Maven 测试阶段工作的临时数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆