【问题标题】:Init database for test purpose during maven test phase在 Maven 测试阶段为测试目的初始化数据库
【发布时间】:2012-07-08 22:05:25
【问题描述】:

我正在尝试执行以下操作:

  • 在 mvn 测试阶段执行一些数据库脚本到 hsqldb

  • 将该数据库用于测试目的

    我能够配置 maven,以便每次调用测试阶段时所有脚本都成功执行,但是(当然有一个 BUT),我所有的测试都失败了。

我的配置:

pom.xml

<build>
    <plugins>
        <!-- Plugin maven for sql -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sql-maven-plugin</artifactId>

            <dependencies>
                <!-- Dependency to jdbc driver -->
                <dependency>
                    <groupId>org.hsqldb</groupId>
                    <artifactId>hsqldb</artifactId>
                    <version>${hsql-version}</version>
                </dependency>
            </dependencies>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
                <encoding>UTF-8</encoding>
                <driver>org.hsqldb.jdbcDriver</driver>
                <url>jdbc:hsqldb:mem:sweetdev_skill_db;shutdown=false</url>
                <settingsKey>hsql-db-test</settingsKey>
                <!--all executions are ignored if -Dmaven.test.skip=true-->
                <skip>${maven.test.skip}</skip>
            </configuration>
            <executions>
                <!--  Create integration test data before running the tests -->
                <execution>
                    <id>create-integration-test-data</id>
                    <phase>process-test-resources</phase>
                    <inherited>true</inherited>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <url>jdbc:hsqldb:mem:db;shutdown=false</url>
                        <autocommit>true</autocommit>
                        <orderFile>ascending</orderFile>
                        <fileset>
                            <basedir>${basedir}/src/test/resources/sql</basedir>
                            <includes>
                                <include>create.sql</include>
                                <include>insert.sql</include>
                            </includes>
                        </fileset>
                    </configuration>
                </execution>

                <!--  Drop data after executing tests -->
                <execution>
                    <id>drop-db-after-test</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <orderFile>ascending</orderFile>
                        <fileset>
                            <basedir>${basedir}/src/test/resources/sql</basedir>
                            <includes>
                                <include>drop.sql</include>
                            </includes>
                        </fileset>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

用于测试的弹簧配置:

<bean id="datasource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver">
    </property>
    <property name="url" value="jdbc:hsqldb:mem:db"></property>
    <property name="username" value="sa">
    </property>
    <property name="password" value="">
    </property>
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datasource" />
    <!-- use testingSetup pu -->
    <property name="persistenceUnitName" value="testingSetup" />
    <property name="persistenceXmlLocation" value="persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
</bean>


<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

输出:

    [INFO] ------------------------------------------------------------------------
    [INFO] [clean:clean]
    [INFO] Deleting directory D:\dev\projects\project-data\target
    [INFO] [resources:resources]
    [INFO] Using default encoding to copy filtered resources.
    [INFO] [compiler:compile]
    [INFO] Compiling 62 source files to D:\dev\projects\project-data\target\classes
    [INFO] [resources:testResources]
    [INFO] Using default encoding to copy filtered resources.
    [INFO] [sql:execute {execution: create-integration-test-data}]
    [INFO] Executing file: D:\dev\projects\project-data\src\test\resources\sql\create.sql
    [INFO] Executing file: D:\dev\projects\project-data\src\test\resources\sql\insert.sql
    [INFO] 230 of 230 SQL statements executed successfully
    [INFO] [compiler:testCompile]
    [INFO] Compiling 12 source files to D:\dev\projects\project-data\target\test-classes
    [INFO] [surefire:test]
    [INFO] Surefire report directory: D:\dev\projects\project-data\target\surefire-reports

但是我所有的测试都失败了。我需要您的帮助来弄清楚它为什么不起作用并帮助我找到解决方案。谢谢


@AndrewLogvinov

这是测试输出之一:

    -------------------------------------------------------------------------------
    Test set: com.ideo.sweetdevskill.data.impl.TestAcquisitionDAOImpl
    -------------------------------------------------------------------------------
    Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.17 sec <<< FAILURE!
    testGetListAcquisitionByUser(com.ideo.sweetdevskill.data.impl.TestAcquisitionDAOImpl)  Time elapsed: 0.148 sec  <<< ERROR!
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.RangeCheck(ArrayList.java:547)
        at java.util.ArrayList.get(ArrayList.java:322)
        at com.ideo.sweetdevskill.data.impl.TestAcquisitionDAOImpl.testGetListAcquisitionByUser(TestAcquisitionDAOImpl.java:100)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
        at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)

谢谢!

【问题讨论】:

    标签: sql spring unit-testing maven hsqldb


    【解决方案1】:

    在您的配置中比较这些行。数据库名称不同,因此使用了两个不同的数据库。

    <url>jdbc:hsqldb:mem:sweetdev_skill_db;shutdown=false</url>
    
    <property name="url" value="jdbc:hsqldb:mem:db"></property>
    

    【讨论】:

    • 再次感谢,但我故意更改了数据库名称以进行编辑(我的意思是,我在写帖子时更改了它,我只是忘记在这部分更改数据库名称是我的应用程序的实际数据库名称)。所以,我实际上使用一个数据库。我现在怀疑的是,要么脚本在我开始运行测试之前回滚(也许它从未提交过:true)要么脚本在其他模式下执行(我不知道为什么) .
    • 您好,现在我在尝试启动 maven 测试阶段时遇到以下错误:ERROR C:org.hibernate.util.JDBCExceptionReporter Table not found in statement
    • 尝试使用具有绝对路径 jdbc:hsqldb:file:/folder/db;shutdown=true 的文件数据库,它在进程终止时存储数据,并允许您查看存储的内容数据库的 .log 文件。
    • 我已经按照你的建议做了,效果很好。我想知道为什么我在使用内存时会出现这些错误。我稍后会为可能需要它的人发布解决方案。再次感谢您的帮助!
    • 我注意到一个非常奇怪的行为,即使在 maven 测试阶段之后删除了我数据库中的所有表之后,我也能够在 eclipse 上使用 junit 运行测试。更奇怪的是,即使我删除了 pom 中的配置(启动所有脚本的配置),maven 测试也成功了。我怀疑一旦我的脚本运行,文件将存储在某个地方。任何想法?谢谢
    【解决方案2】:

    我已经答应发布解决方案,所以我来了。

    让我们从弹簧配置开始

        <?xml version="1.0" encoding="UTF-8"?>
    <!--beans followed by all xml schemas here -->
        <!-- HSQL datasource for test purpose-->
        <bean id="datasource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
            <property name="url" value="jdbc:hsqldb:file:db" />
            <property name="username" value="sa" />
            <property name="password" value="" />
        </bean>
    
        <!-- ============================ ENTITY MANAGER    ================================= -->
        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="datasource" />
            <!-- use testingSetup pu -->
            <property name="persistenceUnitName" value="testingSetup" />
            <property name="persistenceXmlLocation" value="persistence.xml" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="false" />
                    <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
                    <property name="generateDdl" value="false" />
                </bean>
            </property>
        </bean>
    <!-- all other things you may need-->
    </beans>
    

    persistence.xml 将具有与应用程序相同的结构 在项目的 pom.xml 中:(我这里只包括相关部分:

        <!-- Plugin maven for sql -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <dependencies>
                    <!-- Dependency to jdbc driver -->
                    <dependency>
                        <groupId>org.hsqldb</groupId>
                        <artifactId>hsqldb</artifactId>
                        <version>${hsql-version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <encoding>UTF-8</encoding>
                    <driver>org.hsqldb.jdbcDriver</driver>
                    <url>jdbc:hsqldb:file:${basedir}/db;shutdown=true</url>
                    <autocommit>true</autocommit>
                    <settingsKey>hsql-db-test</settingsKey>
                    <!--all executions are ignored if -DskipTests=true-->
                    <skip>${skipTests}</skip>
                </configuration>
                <executions>
                    <!--  Create test data before running the tests -->
                    <execution>
                        <id>create-test-compile-data</id>
                        <phase>process-test-sources</phase>
                        <inherited>true</inherited>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <url>jdbc:hsqldb:file:${basedir}/db;shutdown=true</url>
                            <driver>org.hsqldb.jdbcDriver</driver>
    
                            <orderFile>ascending</orderFile>
                            <detail>true</detail>
                            <fileset>
                                <basedir>${basedir}/src/test/resources/sql</basedir>
                                <includes>
                                    <include>script-create.sql</include>
                                    <include>script-insert.sql</include>
                                </includes>
                            </fileset>
                            <autocommit>true</autocommit>
                        </configuration>
                    </execution>
                    <!--  Drop test data after running the tests include hereafter -->
    
            </plugin>
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 2016-11-10
      • 2015-12-17
      • 2012-08-07
      • 1970-01-01
      相关资源
      最近更新 更多