Results 1 to 4 of 4

Thread: [Issue] JUnit & SpringIOC

  1. #1
    Join Date
    Mar 2008
    Posts
    12

    Default [Issue] JUnit & SpringIOC

    Hi,

    I'm fairly new to Spring and we are currently using Spring's IOC functionality. I'm responsible for creating an ant build that will run our JUnit tests. However, these test run fine in our IDE (Eclipse) but when I run them using the junit ant task I get the following error:

    [junit] junit.framework.TestListener: addError(findByCategoryName, Error creating bean with name 'com.wfm.p3.model.finder.ItemGroupFinderTest': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [com.wfm.p3.model.ItemGroupHome] is defined: Unsatisfied dependency of type [class com.wfm.p3.model.ItemGroupHome]: expected at least 1 matching bean)
    We are using JUnit 4.4 and consequently using the @ContextConfiguration annotation.

    Code:
    @ContextConfiguration(locations={"/testContext.xml"})
    The code seems to find the testContext.xml file just fine but for some reason doesn't do the injection.

    Please let me know if you have any suggestions as to next steps because I'm pretty much lost. Also, since I'm new to this please let me know if I need to provide any other information that will help in resolving this issue.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    424

    Default

    Just a few quick questions to help diagnose your problems:

    1. What version of Ant are you using?
    2. What does your /testContext.xml file look like?
    3. What does your test class look like?


    For the latter two questions, feel free to just include the relevant parts.

    regards,

    Sam

  3. #3
    Join Date
    Mar 2008
    Posts
    12

    Default

    Hi Sam,

    Thanks for the response!

    Question 1: Ant version 1.7.0

    Question 2: We are doing a hack of sorts. Please let me know if you need further clarification.

    Code:
    	<import resource="applicationContext.xml"/>
    
    	<!-- 
    	
    		HACK!   Override 'dataSource' here by executing a jndi-lookup which will fail and return the default 'p3basicDataSource'
    				Note:  This *must* come after the import for 'applicationContext.xml'.  The XML files are parsed first, and 
    				subsequent jndi-lookups with the same id replace previously parsed definitions.
    	
    	 -->	
        <bean id="p3basicDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@host:1521:db"/>
            <property name="username" value="**"/>
            <property name="password" value="**"/>
        </bean>
        
    	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/p3" default-ref="p3basicDataSource"/>
    Question 3:
    Code:
    package com.wfm.p3.model.finder;
    
    
    import java.lang.reflect.Method;
    import java.util.List;
    
    import javax.annotation.Resource;
    import javax.persistence.JoinColumn;
    
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.wfm.p3.model.ItemGroup;
    import com.wfm.p3.model.ItemGroupHome;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/testContext.xml"})
    public class ItemGroupFinderTest {
    
    	@Resource
    	private ItemGroupHome home;
    
    	@Test
    	public void findByCategoryId() {
    		Assert.assertNotNull(home);
    
    		ItemGroupFinder finder = new ItemGroupFinder();
    		finder.setCategoryId(new Long(3));
    		
    		List<ItemGroup> list = home.find(finder);
    		Assert.assertNotNull(list);
    		for(ItemGroup ig: list)
    			System.out.println(ig.getGroupDescription());
    	}
    	
    	@Test
    	public void findByCategoryName() {
    		Assert.assertNotNull(home);
    
    		ItemGroupFinder finder = new ItemGroupFinder();
    		finder.setCategoryName("Produce");
    		
    		System.out.println("go find Produce groups");
    		List<ItemGroup> list = home.find(finder);
    		Assert.assertNotNull(list);
    		for(ItemGroup ig: list)
    			System.out.println(ig.getGroupDescription());
    	}
    }

  4. #4
    Join Date
    Mar 2008
    Posts
    12

    Default

    Well we've narrowed it down to hibernate. Apparently, the table processing/mapping isn't happening. The applicationContext.xml is getting loaded and in it we have the following xml so that the model classes should get processed. So now we need to figure out why the processing isn't happening. Any ideas?

    applicationContext.xml
    Code:
    <context:component-scan base-package="com.wfm.p3.model"/>
    From the ant log:
    Code:
        [junit] 2008-04-23 13:02:34 INFO  test.context.TestContextManager - @TestExecutionListeners is not present for class [class com.wfm.p3.model.AppParameterHomeTest]: using defaults.
        [junit] junit.framework.TestListener: tests to run: 1
        [junit] 2008-04-23 13:02:36 INFO  factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [testContext.xml]
        [junit] 2008-04-23 13:02:38 INFO  factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
        [junit] 2008-04-23 13:02:41 INFO  factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'dataSource': replacing [Generic bean: class [org.springframework.jndi.JndiObjectFactoryBean]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.jndi.JndiObjectFactoryBean]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
        [junit] 2008-04-23 13:02:41 INFO  context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@3c9616: display name [org.springframework.context.support.GenericApplicationContext@3c9616]; startup date [Wed Apr 23 13:02:41 CDT 2008]; root of context hierarchy
        [junit] 2008-04-23 13:02:41 INFO  context.support.GenericApplicationContext - Bean factory for application context [org.springframework.context.support.GenericApplicationContext@3c9616]: org.springframework.beans.factory.support.DefaultListableBeanFactory@fbe184
        [junit] 2008-04-23 13:02:42 INFO  factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fbe184: defining beans [org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,entityManagerFactory,dataSource,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,appParameterHome,dataHome,itemGroupHome,produceItemHome,p3basicDataSource]; root of factory hierarchy
        [junit] 2008-04-23 13:02:48 INFO  springframework.jndi.JndiObjectFactoryBean - JNDI lookup failed - returning specified default object instead: javax.naming.NameNotFoundException: No object bound for java:comp/env/jdbc/p3 [Root exception is java.lang.NullPointerException]
        [junit] 2008-04-23 13:02:49 INFO  orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'punit'
        [junit] 2008-04-23 13:02:49 INFO  cfg.annotations.Version - Hibernate Annotations 3.3.0.GA
        [junit] 2008-04-23 13:02:49 INFO  hibernate.cfg.Environment - Hibernate 3.2.6
        [junit] 2008-04-23 13:02:49 INFO  hibernate.cfg.Environment - hibernate.properties not found
        [junit] 2008-04-23 13:02:49 INFO  hibernate.cfg.Environment - Bytecode provider name : cglib
        [junit] 2008-04-23 13:02:49 INFO  hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
        [junit] 2008-04-23 13:02:49 INFO  hibernate.ejb.Version - Hibernate EntityManager 3.3.1.GA
        [junit] 2008-04-23 13:02:50 INFO  hibernate.ejb.Ejb3Configuration - Processing PersistenceUnitInfo [
        [junit]     name: punit
        [junit]     ...]
    
        TABLE PROCESSING/MAPPING SHOULD BE HAPPENING HERE!
    
        [junit] 2008-04-23 13:02:50 INFO  hibernate.connection.ConnectionProviderFactory - Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
        [junit] 2008-04-23 13:02:50 INFO  ejb.connection.InjectedDataSourceConnectionProvider - Using provided datasource
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
        [junit] With the Partitioning and Data Mining options
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - JDBC driver: Oracle JDBC driver, version: 10.1.0.4.0
        [junit] 2008-04-23 13:02:53 INFO  hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.HSQLDialect
        [junit] 2008-04-23 13:02:53 INFO  hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
        [junit] 2008-04-23 13:02:53 INFO  hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - JDBC batch size: 15
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Connection release mode: auto
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Default batch fetch size: 1
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
        [junit] 2008-04-23 13:02:53 INFO  hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Query language substitutions: {}
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - JPA-QL strict compliance: enabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Second-level cache: enabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Query cache: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.NoCacheProvider
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Statistics: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Default entity-mode: pojo
        [junit] 2008-04-23 13:02:53 INFO  hibernate.cfg.SettingsFactory - Named query checking : enabled
        [junit] 2008-04-23 13:02:54 INFO  hibernate.impl.SessionFactoryImpl - building session factory
        [junit] 2008-04-23 13:02:54 INFO  hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured

Posting Permissions

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