Results 1 to 4 of 4

Thread: Spring Unit Test Failed to Load ApplicationContext

  1. #1
    Join Date
    Aug 2012
    Posts
    11

    Unhappy Spring Unit Test Failed to Load ApplicationContext

    Hi I got a problem related to unit testing. I use Hibernate3 + Spring3.1.1 + JUnit4.1 + Netbeans7.2

    When I try to set up the unit test, it fails when I put "mappingLocations" in the spring application context file. However, the same file is used for deployment and it works perfect fine. If I take "mappingLocations" out, the unit test works without the error below but there is no data can be retrieved from the database.

    Any ideas? Many thanks!!

    The error is shown below.

    Code:
    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@8beff2] to prepare test instance [com.ssl.roamss.test.dao.TestJUnit@1362a63]
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    	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:321)
    	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)
    
    <--omitted-->

    applicationContext.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
         
        <context:component-scan base-package="com.ssl.roamss.database.dao" />
        <context:component-scan base-package="com.ssl.roamss.service" />
        <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close"
              p:driverClassName="org.postgresql.Driver"
              p:url="jdbc:postgresql://1.2.3.4:5432/test"
              p:username="admin"
              p:password="123" />
        
        <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
              lazy-init="true" />
        
        <bean id="sessionFactory" 
              class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
              p:dataSource-ref="dataSource"
              p:mappingLocations="classpath*:/com/ssl/roamss/database/entities/**/*.hbm.xml">
            <property name="packagesToScan">
                <list>
                    <value>
                        com.ssl.roamss.database.entities
                    </value>
                </list>
            </property>
              
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.PostgreSQLDialect
                    </prop>
                    <prop key="hibernate.show_sql">
                        true
                    </prop>
                </props>
            </property>       
        </bean>
        
        <bean id="hibernateTemplate"
              class="org.springframework.orm.hibernate3.HibernateTemplate"
              p:sessionFactory-ref="sessionFactory" />
    </beans>

    My unit test class:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:applicationContext_1.xml"})
    
    public class TestJUnit{
        
    
        @Autowired
        private ConfigDao configDao;
    
        @Test
        public void hello() {
            
            System.out.println(configDao.getAllConfig().get(0).getId().getValue());
        }
    }

    ConfigDao.java

    Code:
    @Repository
    public class ConfigDao extends BaseDao<Config> {
    
        public List<Config> getAllConfig() {
            return this.find("from " + Config.class.getName());
        }
       
    }

  2. #2
    Join Date
    Nov 2007
    Posts
    177

    Default

    Hello,

    -Is your applicationContext_1.xml located at the root of the classpath or in a subdirectory thereof?
    -Is it named applicationContext_1.xml or applicationContext.xml?
    -Why do you mix 2.5 and 3.0 in your namespace declarations?

    J.

  3. #3
    Join Date
    Aug 2012
    Posts
    11

    Default

    Quote Originally Posted by balteo View Post
    Hello,

    -Is your applicationContext_1.xml located at the root of the classpath or in a subdirectory thereof?
    -Is it named applicationContext_1.xml or applicationContext.xml?
    -Why do you mix 2.5 and 3.0 in your namespace declarations?

    J.

    I noticed that mixture and changed all to 3.0. Thanks!

  4. #4
    Join Date
    Aug 2012
    Posts
    11

    Default

    The problem is solved. It is because the Hibernate library in Netbeans 7.2 has an old version of asm.jar. After import all Hibernate jars from download, I got asm-3.1.jar. Then all problems are solved.

Tags for this Thread

Posting Permissions

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