具有内存中SQL(HSQL)的JUnit 4无法正常工作

时间:2018-08-03 07:15:49

标签: spring junit hsqldb

我正在尝试为Junits配置InMemory SQL,在此我遇到以下错误。

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'predictionDaoHelper': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sample.common.dao.Updater com.sample.dao.PredictionDaoHelper.updater; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'updater' defined in class path resource [META-INF/test/common-beans.xml]: Cannot resolve reference to bean 'ifxSessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ifxSessionFactory' defined in class path resource [META-INF/test/common-beans.xml]: Cannot resolve reference to bean 'ifxDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ifxDataSource': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.CompositeDatabasePopulator] while setting bean property 'databasePopulator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.ResourceDatabasePopulator] while setting bean property 'populators' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.config.SortedResourcesFactoryBean] while setting bean property 'scripts'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Post-processing of the FactoryBean's object failed; nested exception is java.lang.AssertionError
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:128)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 25 more

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ifxSessionFactory' defined in class path resource [META-INF/test/common-beans.xml]: Cannot resolve reference to bean 'ifxDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ifxDataSource': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.CompositeDatabasePopulator] while setting bean property 'databasePopulator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.ResourceDatabasePopulator] while setting bean property 'populators' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.config.SortedResourcesFactoryBean] while setting bean property 'scripts'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Post-processing of the FactoryBean's object failed; nested exception is java.lang.AssertionError
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 56 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ifxDataSource': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.CompositeDatabasePopulator] while setting bean property 'databasePopulator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.datasource.init.ResourceDatabasePopulator] while setting bean property 'populators' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.jdbc.config.SortedResourcesFactoryBean] while setting bean property 'scripts'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Post-processing of the FactoryBean's object failed; nested exception is java.lang.AssertionError
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:281)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:125)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 66 more

我的maven依赖项如下

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.1.0.RELEASE</spring.version>
    <log4j.version>1.2.17</log4j.version>
    <hibernate.version>3.6.9.Final</hibernate.version>
    <commons.beanutils.version>1.9.3</commons.beanutils.version>
    <commons.lang.version>2.6</commons.lang.version>
    <junit.version>5.0.0-ALPHA</junit.version>
    <junit.jupiter.version>5.1.0</junit.jupiter.version>
    <junit.vintage.version>5.1.0</junit.vintage.version>
    <junit.platform.version>1.1.0</junit.platform.version>
    <junit.pfm.runner.version>1.1.0</junit.pfm.runner.version>
</properties>
<dependencies>
        <!-- Dependencies For Junits and Mockito- Start  -->
    <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
         <version>2.18.3</version>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>${junit.platform.version}</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.pfm.runner.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> 
        <version>${junit.version}</version> <scope>test</scope> </dependency> -->
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-engine</artifactId>
        <version>${junit.version}</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit.vintage.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- Only required to run tests in an IDE that bundles an older version -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
<!-- Dependencies For Junits and Mockito- End -->   
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.2.3.RELEASE</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>0.8.1</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path-assert</artifactId>
    <version>0.8.1</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.4</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.2.9.Final</version>
    </dependency>
</dependencies>

我的SpringContext文件如下

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


<context:component-scan base-package="com.sample" />

<jdbc:embedded-database id="ifxDataSource" type="HSQL">
        <jdbc:script location="classpath:META-INF/sql/01-schema.sql" />
        <jdbc:script location="classpath:META-INF/sql/02-productseries.sql" />
        <jdbc:script location="classpath:META-INF/sql/03-pidtoseries.sql" />
        <jdbc:script location="classpath:META-INF/sql/04-cloudraycustomization.sql"/>
        <jdbc:script location="classpath:META-INF/sql/05-reports.sql"/>
        <jdbc:script location="classpath:META-INF/sql/06-productseries_cli_mapping.sql"/>
</jdbc:embedded-database>


<bean id="updaterInterceptor" class="com.sample.common.dao.UpdaterInterceptor"/>

<bean id="ifxSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="entityInterceptor" ref="updaterInterceptor"/>
    <property name="dataSource" ref="ifxDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.sample.common.model.common.Alarm</value>
            <value>com.sample.common.model.inventory.Device</value>
            <value>com.sample.common.model.common.DeviceUploadTask</value>
            <value>com.sample.common.model.common.DeviceUploadDetails</value>
            <value>com.sample.common.model.inventory.SSHCredential</value>
            <value>com.sample.common.model.inventory.TelnetCredential</value>
            <value>com.sample.common.model.admin.CloudRayAuth</value>
            <value>com.sample.common.model.admin.SSAPIConnectivityStatus</value>
            <value>com.sample.common.model.messaging.InProgressICRequest</value>
            <value>com.sample.common.model.messaging.ICRecommendedAction</value>
            <value>com.sample.common.model.messaging.ICCustomRecommendedAction</value>
            <value>com.sample.common.model.common.BaseBusinessObject</value>
            <value>com.sample.common.model.controller.CloudRayRequest</value>
            <value>com.sample.common.model.controller.Handler</value>
            <value>com.sample.common.model.controller.Stage</value>
            <value>com.sample.common.model.customization.CloudRayCustomization</value>
            <value>com.sample.common.model.events.Command</value>
            <value>com.sample.common.model.events.DiagnosticSignaturesAdvice</value>
            <value>com.sample.common.model.events.ProductSeries</value>
            <value>com.sample.common.model.events.ProductIdToSeries</value>
            <value>com.sample.common.model.events.ShowCommands</value>
            <value>com.sample.common.model.events.SyslogClassification</value>
            <value>com.sample.common.model.report.Report</value>
            <value>com.sample.common.model.common.EmailRequestTracker</value>
            <value>com.sample.common.model.common.DroppedAlarm</value>
            <value>com.sample.common.model.events.Signature</value>
            <value>com.sample.common.model.admin.UserDetail</value>
            <value>com.sample.common.model.login.UserSecurityQuestions</value>
            <value>com.sample.common.model.login.UserSecurityAnswers</value>
            <value>com.sample.common.model.events.TimerRunDate</value>
            <value>com.sample.common.model.common.AlarmICRecommendedAction</value>
            <value>com.sample.common.model.events.DiagnosticSignaturesAdviceCommand</value>
            <value>com.sample.common.model.messaging.GlobalicRundate</value>
            <value>com.sample.common.model.common.FailedAlarm</value>
            <value>com.sample.common.model.common.SyslogHeartBeat</value>
            <value>com.sample.model.config.CspcConfiguration</value>
            <value>com.sample.model.config.ProxyConfiguration</value>
            <value>com.sample.common.model.common.DroppedSyslogMessages</value>
            <value>com.sample.common.model.events.ProductSeriesTech</value>
            <value>com.sample.common.model.admin.DeviceProfile</value>
            <value>com.sample.common.model.events.CSPCProductSeries</value> 
            <value>com.sample.common.model.events.CSPCDeviceResponse</value>
            <value>com.sample.common.model.common.DeviceBatchProcessingDetails</value>
            <value>com.sample.model.config.SmtpMailServerConfiguration</value>
            <value>com.sample.common.model.events.CliCommand</value>
            <value>com.sample.common.model.common.GenericAlarmAuditLog</value>
            <value>com.sample.common.model.events.NewPatterns</value>
            <value>com.sample.common.model.common.NewPrediction</value>
            <value>com.sample.common.model.cspc.CspcConnectivityDetails</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.jdbc.batch_size">${HIBERNATE_BATCH_SIZE}</prop>
            <prop key="hibernate.order_inserts">${HIBERNATE_ORDER_INSERTS}</prop>
            <prop key="hibernate.order_updates">${HIBERNATE_ORDER_UPDATES}</prop>
        </props>
    </property>
</bean>

<bean id="updater" class="com.sample.common.dao.Updater">
        <property name="sessionFactory" ref="ifxSessionFactory"/>
</bean>


</beans>

0 个答案:

没有答案