2

我正在尝试为我的应用程序运行测试,但不使用生产数据库,而是使用内存中的 Hypersonic。

我遇到的问题是,在最终让它使用我希望它使用的 applicationContext.xml 和 persistence.xml 文件之后,我收到了与 CatalogItem 类相关的测试的以下错误。

问题是我被明确告知我不需要修改类本身的任何内容,因为它们在 MySQL 上运行得很好。我希望问题出在我的 .xml 文件上。

测试持久性.xml

<!-- Classes declaration omitted -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


    <!-- FOR UNIT TESTING ONLY -->

    <context:property-placeholder location="classpath*:META-INF/spring/test-database.properties"/>

    <bean id="externalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
       <property name="ignoreResourceNotFound"><value>true</value></property>
       <property name="locations">
          <list>
            <value>classpath*:META-INF/conf/configuration.properties</value>
          </list>
       </property>
    </bean>

    <context:spring-configured/>

    <context:component-scan base-package="com.mms.pone.portal">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

     <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="testWhileIdle" value="false"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="maxActive" value="1" />

        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>

        <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" />
    </bean>    
</beans>

数据库属性:

########################################################
#                                                      #
#  This configuration is used for unit-testing only.   #
#                                                      #
########################################################
# Hypersonic
database.driverClassName=org.hsqldb.jdbcDriver

database.url=jdbc\:hsqldb\:file\:c\:/Project

database.username=sa
database.password=

还有我在运行 mvn 时得到的堆栈跟踪

Tests in error:
  testDeleteAsset(com.mms.pone.portal.domain.AssetIntegrationTest): org.hibernate.exception.SQLGrammarException: could not load an entity: [com.mms.pone.portal.domain.Asset#64]; nested exception is ja
vax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.mms.pone.portal.domain.Asset#64]
  testFindCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceExc
eption: org.hibernate.exception.SQLGrammarException: could not execute query
  testCountAllCatalogItems(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persisten
ceException: org.hibernate.exception.SQLGrammarException: could not execute query
  testFindAllCatalogItems(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persistenc
eException: org.hibernate.exception.SQLGrammarException: could not execute query
  testFindCatalogItemEntries(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persist
enceException: org.hibernate.exception.SQLGrammarException: could not execute query
  testUpdateCatalogItemUpdate(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.Persis
tenceException: org.hibernate.exception.SQLGrammarException: could not execute query
  testSaveCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceExc
eption: org.hibernate.exception.SQLGrammarException: could not execute query
  testDeleteCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceE
xception: org.hibernate.exception.SQLGrammarException: could not execute query
  testFlush(com.mms.pone.portal.domain.CatalogItemIntegrationTest): org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: or
g.hibernate.exception.SQLGrammarException: could not execute query

CatalogItemIntegrationTest 的日志输出:

-------------------------------------------------------------------------------
Test set: com.mms.pone.portal.domain.CatalogItemIntegrationTest
-------------------------------------------------------------------------------
Tests run: 9, Failures: 0, Errors: 8, Skipped: 0, Time elapsed: 0.024 sec <<< FAILURE!
testFlush(com.mms.pone.portal.domain.CatalogItemIntegrationTest)  Time elapsed: 0.015 sec  <<< ERROR!
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:326)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.findCatalogItemEntries_aroundBody6(CatalogItem_Roo_Jpa_ActiveRecord.aj:31)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethod$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj:1)
    at com.mms.pone.portal.domain.CatalogItem.findCatalogItemEntries(CatalogItem.java:1)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj)
    at com.mms.pone.portal.service.CatalogItemServiceImpl_Roo_Service.ajc$interMethod$com_mms_pone_portal_service_CatalogItemServiceImpl_Roo_Service$com_mms_pone_portal_service_CatalogItemServiceImpl$findCatalogItemEntries(CatalogItemServiceImpl_Roo_Service.aj:35)
    at com.mms.pone.portal.service.CatalogItemServiceImpl.findCatalogItemEntries(CatalogItemServiceImpl.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:159)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.init(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:146)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.getRandomCatalogItem(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
    at com.mms.pone.portal.domain.CatalogItemIntegrationTest_Roo_IntegrationTest.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemIntegrationTest_Roo_IntegrationTest$com_mms_pone_portal_domain_CatalogItemIntegrationTest$testFlush(CatalogItemIntegrationTest_Roo_IntegrationTest.aj:74)
    at com.mms.pone.portal.domain.CatalogItemIntegrationTest.testFlush(CatalogItemIntegrationTest.java:1)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255)
    ... 48 more
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2545)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
    ... 48 more
Caused by: java.sql.SQLException: Table not found in statement [select top ? catalogite0_.id as id6_, catalogite0_.active as active6_, catalogite0_.category_id as category14_6_, catalogite0_.deliveryTime as delivery3_6_, catalogite0_.descriptionLong as descript4_6_, catalogite0_.descriptionShort as descript5_6_, catalogite0_.ean as ean6_, catalogite0_.externalFormId as external7_6_, catalogite0_.manufacturer_id as manufac15_6_, catalogite0_.name as name6_, catalogite0_.remarks as remarks6_, catalogite0_.sapMatNr as sapMatNr6_, catalogite0_.supplierAid as supplie11_6_, catalogite0_.supplier_catalog_id as supplier16_6_, catalogite0_.type as type6_, catalogite0_.version as version6_ from CatalogItem catalogite0_]
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:313)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
    at org.hibernate.loader.Loader.doQuery(Loader.java:801)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.doList(Loader.java:2542)
    ... 56 more
testFindCatalogItem(com.mms.pone.portal.domain.CatalogItemIntegrationTest)  Time elapsed: 0 sec  <<< ERROR!
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:326)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.findCatalogItemEntries_aroundBody6(CatalogItem_Roo_Jpa_ActiveRecord.aj:31)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethod$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj:1)
    at com.mms.pone.portal.domain.CatalogItem.findCatalogItemEntries(CatalogItem.java:1)
    at com.mms.pone.portal.domain.CatalogItem_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItem_Roo_Jpa_ActiveRecord$com_mms_pone_portal_domain_CatalogItem$findCatalogItemEntries(CatalogItem_Roo_Jpa_ActiveRecord.aj)
    at com.mms.pone.portal.service.CatalogItemServiceImpl_Roo_Service.ajc$interMethod$com_mms_pone_portal_service_CatalogItemServiceImpl_Roo_Service$com_mms_pone_portal_service_CatalogItemServiceImpl$findCatalogItemEntries(CatalogItemServiceImpl_Roo_Service.aj:35)
    at com.mms.pone.portal.service.CatalogItemServiceImpl.findCatalogItemEntries(CatalogItemServiceImpl.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:159)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.init(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$init(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj:146)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand.getRandomCatalogItem(CatalogItemDataOnDemand.java:1)
    at com.mms.pone.portal.domain.CatalogItemDataOnDemand_Roo_DataOnDemand.ajc$interMethodDispatch1$com_mms_pone_portal_domain_CatalogItemDataOnDemand_Roo_DataOnDemand$com_mms_pone_portal_domain_CatalogItemDataOnDemand$getRandomCatalogItem(CatalogItemDataOnDemand_Roo_DataOnDemand.aj)
4

2 回答 2

1

未找到表“CatalogItem”:

Table not found in statement [select top ? [...] from CatalogItem catalogite0_]

所以也许你对属性“hibernate.hbm2ddl.auto”的设置不正确。你可以试试“hibernate.hbm2ddl.auto=update”,也许有帮助。或者您可以使用 dbmaintain - http://dbmaintain.org/overview.html - 而不是“hibernate.hbm2ddl.auto”。无论如何,它更安全,更灵活。

如果这没有帮助,请尝试启用 SQL 日志记录并查看生成的 SQL 语句。将以下属性添加到您的 persistence.xml:

<property name="hibernate.show_sql" value="true"/> 
<property name="hibernate.format_sql" value="true"/> <property name="use_sql_comments" value="true"/> 

编辑如果您使用 Squirrel 之类的工具检查 HSQL 数据库,它也可能会有所帮助:http: //squirrel-sql.sourceforge.net/您可以使用它检查所有表是否正确创建。

于 2012-11-12T08:15:40.667 回答
0

虽然不是我最初想要的,但在切换到 H2-IN-MEMORY 后测试运行完美......

于 2012-11-15T12:54:05.217 回答