Junit配置文件Spring MVC

时间:2014-05-19 13:18:25

标签: xml spring junit

我的项目中的Junit配置文件存在问题。

我的应用程序配置文件application-context.xml如下所示:

<context:component-scan base-package="demo" />
<context:annotation-config />

<!-- Defines the entity manager factory -->
<bean id='entityManagerFactory'
    class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
    <property name="persistenceUnitName" value="Skelet" />
    <property name='dataSource' ref='dataSource' />
    <property name="jpaVendorAdapter">
        <bean id="jpaAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <!--<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" 
                /> -->
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
            <!--<property name="databasePlatform" value="org.hibernate.dialect.Oracle9Dialect" 
                /> -->
            <property name="generateDdl" value="true" />
            <property name="showSql" value="false" />
        </bean>
    </property>
</bean>

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

<tx:annotation-driven transaction-manager="txManager" />

我为junit创建了一个新配置文件并创建了一个用于测试的内存数据库junit-context.xml,如下所示:

<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd">

<jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:sql/model_script.sql"/>
</jdbc:embedded-database>

</beans>

model_scrips / sql是一个创建数据库并将数据放入其中的脚本。

我注释了这样的测试类:

@ContextConfiguration(locations = { "classpath:config/application-context.xml",   "classpath:config/junit-context.xml" })

我的想法是从应用程序上下文中获取配置,并仅覆盖用于测试的数据库的数据源。

当我尝试运行测试时出现此错误:

    <?xml version="1.0" encoding="UTF-8"?><testrun name="demo (9)" project="demo"   tests="56" started="56" failures="0" errors="56" ignored="0">
  <testsuite name="demo.repository.RoomRepositoryTest" time="0.636">
  <testcase name="testIfGettingRoomById" classname="demo.repository.RoomRepositoryTest" time="0.479">
  <error>java.lang.IllegalStateException: Failed to load ApplicationContext&#13;
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)&#13;
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)&#13;
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)&#13;
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)&#13;
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)&#13;
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)&#13;
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)&#13;
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)&#13;
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)&#13;
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)&#13;
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)&#13;
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)&#13;
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)&#13;
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)&#13;
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)&#13;
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)&#13;
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)&#13;
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)&#13;
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)&#13;
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)&#13;
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)&#13;
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)&#13;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)&#13;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)&#13;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)&#13;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)&#13;
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in XML document from class path resource [config/junit-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 45; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jdbc:embedded-database'.&#13;
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)&#13;
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)&#13;
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)&#13;
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)&#13;
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)&#13;
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)&#13;
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)&#13;
at org.springframework.test.context.web.GenericXmlWebContextLoader.loadBeanDefinitions(GenericXmlWebContextLoader.java:38)&#13;
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:125)&#13;
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)&#13;
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)&#13;
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)&#13;
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)&#13;
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)&#13;
... 25 more&#13;
Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 45; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jdbc:embedded-database'.&#13;
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)&#13;
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)&#13;
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:458)&#13;
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3237)&#13;
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1917)&#13;
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:378)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2770)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)&#13;
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)&#13;
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)&#13;
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)&#13;
at    com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)&#13;
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)&#13;
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)&#13;
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)&#13;
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)&#13;
... 38 more&#13;

2 个答案:

答案 0 :(得分:3)

junit-context.xml内容使用以下内容:

<?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: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.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:sql/model_script.sql"/>
    </jdbc:embedded-database>

</beans>

答案 1 :(得分:0)

解决这个问题?取自another Tread Spring上下文文件中的这一行:

  

的xmlns:JDBC = “http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd”

应更改为:

  

的xmlns:JDBC = “http://www.springframework.org/schema/jdbc”

不确定您正在使用哪种IDE,但有一些(例如IntelliJ),这会被标记为错误并节省很多头痛!