2012-07-09 49 views
1

我努力做到以下幾點:初始化數據庫測試的目的

  • 執行期間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) 

謝謝!

+0

測試輸出說什麼? – 2012-07-09 13:54:44

回答

4

比較你的配置中的這些行。數據庫名稱不同,因此正在使用兩個不同的數據庫。

<url>jdbc:hsqldb:mem:sweetdev_skill_db;shutdown=false</url> 

<property name="url" value="jdbc:hsqldb:mem:db"></property> 
+0

再次感謝,但我有意改變了數據庫名稱以便編輯目的(由t我的意思是,我在寫我的帖子的時候改變了它,我只是忘記了更改數據庫名稱,這是我的應用程序的實際數據庫名稱)。所以,我實際上使用了一個數據庫。我現在懷疑的是在開始運行我的測試之前回滾腳本(可能它從來沒有提交,儘管如此: true)腳本在其他架構下執行(我不知道爲什麼)。 – aslan 2012-07-10 08:21:05

+0

您好,現在我在嘗試啓動maven測試階段時出現以下錯誤:錯誤C:org.hibernate.util.JDBCExceptionReporter在聲明中找不到表 – aslan 2012-07-10 11:50:17

+0

嘗試使用具有absulute路徑的文件數據庫jdbc:hsqldb:file :/ folder/db; shutdown = true,它在進程終止時存儲數據,並允許您查看正在存儲的內容。數據庫的日誌文件。 – fredt 2012-07-10 14:06:18

0

我已承諾發佈解決方案,所以我在這裏。

讓我們開始與部份Sp​​ring配置

<?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> 

感謝您的幫助

+0

這就是我解決上面提到的問題的方法! – aslan 2012-07-11 09:31:44