我正在尝试执行以下操作:
将该数据库用于测试目的
我能够配置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>
spring test for test:
<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 :(得分:4)
在配置中比较这些行。数据库名称不同,因此使用了两个不同的数据库。
<url>jdbc:hsqldb:mem:sweetdev_skill_db;shutdown=false</url>
<property name="url" value="jdbc:hsqldb:mem:db"></property>
答案 1 :(得分:0)
我答应发布解决方案,所以我在这里。
让我们从春季配置开始
<?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>
感谢您的帮助。