2014-12-29 68 views
0

我需要一個小問題的解決方案。我正在關注this教程和這個source code。我使用maven,eclipse作爲教程,並使用默認的maven結構。我把這一切的方式都放在這個教程中。所以,我的階級結構是這樣的無法加載Spring Web應用程序的ApplicationContext

src/main/java 
src/main/resources/spring/business-config.xml 
src/test/java/tutorial/core/repositories/AccountRepoTest.java 

這裏是我的測試類

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:spring/business-config.xml") 
public class AccountRepoTest { 

    @Autowired 
    private AccountRepo repo; 

    private Account account; 

    @Before 
    @Transactional 
    @Rollback(false) 
    public void setup(){ 
     account=new Account(); 
     account.setName("name"); 
     account.setPassword("password"); 
     repo.createAccount(account); 
    } 

    @Test 
    @Transactional 
    public void testFind(){ 
     assertNotNull(repo.findAccount(account.getId())); 
    } 
} 

問題是,當我運行測試,它顯示應用程序上下文加載失敗。但我已經按照教程和視頻教程所做的一切顯示ok了。本教程的其他測試(未使用ApplicationContext)運行良好。我假設在我的eclipse classpath中存在一些問題。我想知道,我該如何解決這個問題?我給我的生意-config.xml中及以下控制檯輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <context:component-scan base-package="tutorial.core.repositories.jpa" /> 

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> 
     <property name="username" value="sa" /> 
     <property name="password" value="sa" /> 
     <property name="url" value="jdbc:h2:mem:test" /> 
     <property name="driverClassName" value="org.h2.Driver" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactory"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdaptor"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateVendorAdaptor" /> 
     </property> 
     <property name="jpaProperties"> 
      <map> 
       <entry key="hibernate.hbm2ddl.auto" value="create-drop" /> 
       <entry key="hibernate.show_sql" value="true" /> 
      </map> 
     </property> 
     <property name="packagesToScan" value="tutorial.core.model.entities" /> 
    </bean> 

    <tx:annotation-driven /> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" /> 
</beans> 

控制檯輸出:

Dec 29, 2014 10:16:03 PM org.springframework.test.context.TestContextManager retrieveTestExecutionListeners 
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 
Dec 29, 2014 10:16:05 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [spring/business-config.xml] 
Dec 29, 2014 10:16:10 PM org.springframework.test.context.TestContextManager prepareTestInstance 
SEVERE: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]d1af89] to prepare test instance [[email protected]] 
java.lang.IllegalStateException: Failed to load ApplicationContext 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) 
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    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:309) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    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:675) 
    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.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx] 
Offending resource: class path resource [spring/business-config.xml] 

    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) 
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85) 
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80) 
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316) 
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1421) 
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1414) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:141) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:110) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:253) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:122) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250) 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) 
    ... 25 more 
+0

什麼是企業-config.xml中的內容? – SMA

+0

我已將它添加到問題中。 @almas shaikh – Mahin

+0

你是否在使用spring和transaction manager的mvc特性? – SMA

回答

3

這適用於我所有的JUnit測試。您可能不需要@WebAppConfiguration。你可能會也可能不想連線這些@PropertySrouce文件,但相信我他們會派上用場。

右鍵單擊 - >上下文文件的屬性,以確保您有正確的路徑。

祝你好運!

@Configuration 
 
    @PropertySource(value={ 
 
\t \t "classpath:usermgmt.properties", 
 
\t \t "classpath:ldap.properties", 
 
\t \t 
 
    }) 
 
    @ContextConfiguration(locations = { 
 
\t \t "file:src/main/webapp/WEB-INF/spring-config.xml", 
 
\t \t "file:src/main/webapp/WEB-INF/conf/applicationContext-email.xml", 
 
\t \t "file:src/main/webapp/WEB-INF/conf/applicationContext-jdbc.xml" 
 
\t \t 
 
    }) 
 
    @WebAppConfiguration 
 
    @RunWith(SpringJUnit4ClassRunner.class) 
 
    public class ClassTest { 
 

 
    }

只是一個更新使用Springboot那些我套上了對JUnit和引導。 下面是你需要的JUnit和Springboot:

@RunWith(SpringJUnit4ClassRunner.class) 
 
@SpringApplicationConfiguration(classes = ExampleApplication.class) 
 
@TestPropertySource(locations="classpath:test.properties") 
 
public class ExampleApplicationTests { 
 

 
}

相關問題