Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Newbie : AbstractDependencyInjectionSpringContextTests and Spring xml file

  1. #1
    Join Date
    Oct 2006
    Posts
    100

    Default Newbie : AbstractDependencyInjectionSpringContextTests and Spring xml file

    I'm trying to build my first Spring junit test by employing the 'AbstractDependencyInjectionSpringContextTests' class as the parent class.

    The junit code is failing because it can't find the Spring xml file. I have this located under a 'conf' sub folder. I have tried both...

    protected String[] getConfigLocations() {
    return new String[] {"conf/applicationContext.xml"};
    }

    protected String[] getConfigLocations() {
    return new String[] {"applicationContext.xml"};
    }

    ... and both cases I get a "file cannot be found error".

  2. #2
    Join Date
    Oct 2006
    Posts
    100

    Default

    By changing the string value to...

    protected String[] getConfigLocations() {
    return new String[] {"classpath:applicationContext.xml"};
    }

    ... I can get past my initial problem. But it now fails trying to load my Hibernate bean xml file. The related Hibernate bean definition from the applocationContext.xml file is shown below,,,

    <bean id="hibernate_session_factory"
    class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource">
    <ref bean="MySQL_sor_risk_1000" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>
    com/eis/sor/hibernate/mysql/FinancialAccount.hbm.xml
    </value>
    </list>
    </property>
    </bean>

    My 'FinancialAccount.hbm.xml' file is in a source folder 'com.eis.sor.hibernate.mysql'.

    Any ideas?

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    In which way does it fail? An error would give more information. And please when posting code/stacktraces/etc use [ code] [ /code] tags.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Oct 2006
    Posts
    100

    Default

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'hibernate_session_factory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: class path resource [FinancialAccount.hbm.xml] cannot be opened because it does not exist
    java.io.FileNotFoundException: class path resource [FinancialAccount.hbm.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getI nputStream(ClassPathResource.java:137)
    at org.springframework.orm.hibernate3.LocalSessionFac toryBean.afterPropertiesSet(LocalSessionFactoryBea n.java:679)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1091)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:396)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:233)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:145)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:277)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:313)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:87)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:72)
    at org.springframework.test.AbstractSpringContextTest s.loadContextLocations(AbstractSpringContextTests. java:135)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.loadContextLocations(Abstract DependencyInjectionSpringContextTests.java:224)
    at org.springframework.test.AbstractSpringContextTest s.getContext(AbstractSpringContextTests.java:115)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:192)
    at junit.framework.TestCase.runBare(TestCase.java:125 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

  5. #5
    Join Date
    Oct 2006
    Posts
    100

    Default

    Reading thru 'Spring in Action' I've found that I missing a couple of beans, but it still fails.

    The applicationContext.xml file is...

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    
    	<bean id="mySQL" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName">
    			<value>com.mysql.jdbc.Driver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:mysql://localhost:3306/sor_risk_1000</value>
    		</property>
    		<property name="username">
    			<value>root</value>
    		</property>
    		<property name="password">
    			<value>root</value>
    		</property>
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="mySQL" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLDialect
    				</prop>
    			</props>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>classpath:/com/eis/sor/hibernate/mysql</value>
    			</list>
    		</property>
    	</bean>
    
    	<bean id="hibernateTemplate"
    		class="org.springframework.orm.hibernate.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="FinanceAccountManagerDAO" name="FinanceAccountManagerDAO"
    		class="com.eis.sor.finance.account.HibernateFinancialAccountManagerDAO"
    		abstract="false" singleton="true" lazy-init="default"
    		autowire="default" dependency-check="default">
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate" />
    		</property>
    
    	</bean>
    	<bean id="SorFinanceAccountManager" name="FinanceAccountManager"
    		class="com.eis.sor.finance.account.FinancialAccountManagerImpl"
    		abstract="false" singleton="true" lazy-init="default"
    		autowire="default" dependency-check="default">
    		<property name="DAO">
    			<ref bean="FinanceAccountManagerDAO" />
    		</property>
    	</bean>
    </beans>
    The 'FinancialAccount.hbm.xml' is...

    Code:
    <hibernate-mapping auto-import="true">
        <class name="com.eis.sor.finance.account.FinancialAccount" 
               table="financial_account" 
               catalog="sor_risk_1000">
            <id name="iD" type="java.lang.Integer" unsaved-value="-1" >
                <column name="FinAccountID" />
                <generator class="native" />
            </id>
            <property name="title" type="java.lang.String">
                <column name="FinAccountTitle" length="50" not-null="true" unique="true" />
            </property>
            <property name="desc" type="java.lang.String">
                <column name="FinAccountDesc" length="250" />
            </property>
            <property name="displayOrder" type="java.lang.Integer">
                <column name="FinAccountDisplayOrder" not-null="true" />
            </property>
        </class>
    </hibernate-mapping>
    The error is reported as...

    Code:
    org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'hibernateTemplate' defined in class path resource [applicationContext.xml]: Class that bean class [org.springframework.orm.hibernate.HibernateTemplate] depends on not found; nested exception is java.lang.NoClassDefFoundError: net/sf/hibernate/HibernateException
    java.lang.NoClassDefFoundError: net/sf/hibernate/HibernateException
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Unknown Source)
    	at org.springframework.util.ClassUtils.forName(ClassUtils.java:109)
    	at org.springframework.beans.factory.support.BeanDefinitionReaderUtils.createBeanDefinition(BeanDefinitionReaderUtils.java:65)
    	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitionElement(DefaultXmlBeanDefinitionParser.java:466)
    	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitionElement(DefaultXmlBeanDefinitionParser.java:432)
    	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitions(DefaultXmlBeanDefinitionParser.java:347)
    	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.registerBeanDefinitions(DefaultXmlBeanDefinitionParser.java:197)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:295)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:223)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:173)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:148)
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:129)
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:145)
    	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
    	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:81)
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:89)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:262)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:87)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:72)
    	at org.springframework.test.AbstractSpringContextTests.loadContextLocations(AbstractSpringContextTests.java:135)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.loadContextLocations(AbstractDependencyInjectionSpringContextTests.java:224)
    	at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:115)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:192)
    	at junit.framework.TestCase.runBare(TestCase.java:125)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Edit:

    You have a missing jar file. You are probably extending org.springframework.orm.hibernate.HibernateDaoSupp ort assuming you are using Hibernate 3 change that to org.springframework.orm.hibernate3.HibernateDaoSupport. The latter one is for use with Hibernate 2 instead of 3.

    Next you don't need to inject a HibernateTemplate you can simple inject the sessionfactory.

    Furthermore apparently it cannot find the file as it says in the stacktrace

    Code:
    java.io.FileNotFoundException: class path resource [FinancialAccount.hbm.xml] cannot be opened because it does not exist
    You can also add the classpath: part to your mappingresources, which should take care of that issue.
    Last edited by Marten Deinum; Oct 31st, 2006 at 05:43 AM. Reason: Rework with additional info :)
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Oct 2006
    Posts
    100

    Default

    My Hiberante DAO file was using the right package...

    Code:
    package com.eis.sor.finance.account;
    
    import java.util.Iterator;
    import java.util.List;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    public class HibernateFinancialAccountManagerDAO extends HibernateDaoSupport
          implements FinancialAccountManagerDAO {
    I've changed back to the 'mappingResources' property and it says it can't find the hbm.xml file...

    Code:
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="mySQL" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLDialect
    				</prop>
    			</props>
    		</property>
    		<property name="mappingResources">
    			<list>
    				<value>classpath:FinancialAccount.hbm.xml</value>
    			</list>
    		</property>
    	</bean>
    The error is...

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: class path resource [classpath:FinancialAccount.hbm.xml] cannot be opened because it does not exist
    java.io.FileNotFoundException: class path resource [classpath:FinancialAccount.hbm.xml] cannot be opened because it does not exist
    	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:679)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1091)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:396)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:277)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:87)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:72)
    	at org.springframework.test.AbstractSpringContextTests.loadContextLocations(AbstractSpringContextTests.java:135)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.loadContextLocations(AbstractDependencyInjectionSpringContextTests.java:224)
    	at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:115)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:192)
    	at junit.framework.TestCase.runBare(TestCase.java:125)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Just rechecked your config and you are trying to use the org.springframework.orm.hibernate.HibernateTemplat e instead of the org.springframework.orm.hibernate3.HibernateTemplate one.

    You stated that you file was located in some directory you should also include that in your resourcemappings.

    Code:
    <property name="mappingResources">
    <list>
    <value>classpath:/com/eis/sor/hibernate/mysql/FinancialAccount.hbm.xml</value>
    </list>
    </property>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Aug 2004
    Location
    Burgas, Bulgaria
    Posts
    37

    Default

    Describe your build environment. Eclipse, Maven, maven2, ant? Describe your test environment as well.

  10. #10
    Join Date
    Oct 2006
    Posts
    100

    Default

    I'm a relative Eclipse newbie as well, so advance apologies for any dumb questions...

    I've attached a screeen shot of the Eclipse package explorer.

    I've managed to get this to work by making the two directories where the xml files are stored as 'src' folders.

    The latest 'applicationContext.xml' content is...

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    
    	<bean id="mySQL" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName">
    			<value>com.mysql.jdbc.Driver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:mysql://localhost:3306/sor_risk_1000</value>
    		</property>
    		<property name="username">
    			<value>root</value>
    		</property>
    		<property name="password">
    			<value>root</value>
    		</property>
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="mySQL" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLDialect
    				</prop>
    			</props>
    		</property>
    		<property name="mappingResources">
    			<list>
    				<value>FinancialAccount.hbm.xml</value>
    			</list>
    		</property>
    	</bean>
    
    	<bean id="hibernateTemplate"
    		class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    
    
    	<bean id="FinanceAccountManagerDAO" name="FinanceAccountManagerDAO"
    		class="com.eis.sor.finance.account.HibernateFinancialAccountManagerDAO"
    		abstract="false" singleton="true" lazy-init="default"
    		autowire="default" dependency-check="default">
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate" />
    		</property>
    	</bean>
    
    	<bean id="SorFinanceAccountManager" name="FinanceAccountManager"
    		class="com.eis.sor.finance.account.FinancialAccountManagerImpl"
    		abstract="false" singleton="true" lazy-init="default"
    		autowire="default" dependency-check="default">
    		<property name="DAO">
    			<ref bean="FinanceAccountManagerDAO" />
    		</property>
    	</bean>
    </beans>
    The test unit code is....

    Code:
    import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    
    import com.eis.sor.finance.account.FinanceAccount;
    import com.eis.sor.finance.account.FinancialAccountManagerImpl;
    
    public class FinanceManagerTest extends
          AbstractDependencyInjectionSpringContextTests {
    
       private FinancialAccountManagerImpl fService = null;
       
       @Override
       protected String[] getConfigLocations() {
          return new String[] {"classpath:applicationContext.xml"};
       }
       /**
        * Test method for {@link com.eis.sor.finance.account.FinancialAccountManagerImpl#add(com.eis.sor.finance.account.FinanceAccount)}.
        */
    It's not clear to me why I need to make the folders where the xml files are located as 'src folders' when both locations are under the normal src root.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •