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

查看:48
本文介绍了创建跨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-plugin .它可以从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>

有关更多信息,您还可以检查使用情况.

For more info you can also check the USAGE.

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

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