Announcement Announcement Module
Collapse
No announcement yet.
UnUsual dependancy injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • UnUsual dependancy injection

    Hi

    we are using Spring and hibernate in our project. We have few test cases which is throwing unusualdependancyinjection reason Unable to set datasource. These are my configuraion files:

    UserDaoTestCase
    Code:
    public class UserDaoTestCase extends AbstractTransactionalTest {
    
        private SecurityDao securityDao;
    
        protected void onSetUpBeforeTransaction() throws Exception {
            securityDao = ( SecurityDao) applicationContext.getBean("securityDao");
        }
    
        public void testFindAllUsers() {
            List<User> users = securityDao.findAllUsers();
            assertNotNull("Expected a list of users", users);
            for ( User user : users ) {
                securityDao.deleteUser(user);
            }
        }
    }
    AbstractTransactionalTest
    Code:
    public abstract class AbstractTransactionalTest extends AbstractTransactionalDataSourceSpringContextTests
    {
        private final static String[] contextPaths = {
                "mercury-app.xml",
                "mercury-servlet.xml",
                "mercury-data.xml",
               };
    
        protected String[] getConfigLocations() {
            return contextPaths;
        }
    
        @Override
    	protected void onSetUpBeforeTransaction() throws Exception {
    		super.onSetUpBeforeTransaction();
    		User user = new User();
    		user.setFirstName("JUnit Test User");
    		AuthenticatedUser.setAuthenticatedUser(user);
    	}
    }
    Code:
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" destroy-method="close">
           <property name="jndi">com.test.mercury</property>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                    <prop key="hibernate.use_outer_join">true</prop>
                    <prop key="hibernate.query.substitutions">true=1 false=0</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <!--prop key="hibernate.cache.use_query_cache">true</prop-->
                    <!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
                </props>
            </property>
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
    Code:
    <bean id="securityDao" class="com.primerevenue.mercury.dao.impl.SecurityDaoImpl">
            <property name="sessionFactory"><ref bean="sessionFactory"/></property>
        </bean>

  • #2
    It would be really useful to see the stracktrace.

    Comment


    • #3
      It seems that you should be setting the "jndiName" property and not "jndi".

      Try changing
      Code:
      <property name="jndi">com.test.mercury</property>
      to

      Code:
      <property name="jndiName" value="com.test.mercury"/>
      I am, of course, assuming that your JNDI object is bound to com.test.mercury name.

      -Ramnivas

      Comment


      • #4
        org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'com.ttp.dao.UserDaoTestCase' defined in null: Unsatisfied dependency expressed through bean property 'dataSource': set this property value or disable dependency checking for this bean
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:962)
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:823)
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:200)
        at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:187)
        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)

        Comment


        • #5
          Yes jndi object is bound to com.test.mercury

          I have property name set as jndiName. I have posted the stack trace if it is of any use.

          Comment


          • #6
            Which applicationContext.xml file is the dataSource bean in? Spring obviously can't see to find it which would suggest you haven't included the file that contains it.

            Comment


            • #7
              dataSource bean is configured in mercury-data.xml
              Dao's and Service's are configured in mercury-app.xml

              Comment


              • #8
                Hmmm, I've just tried the same thing on my machine and it works fine. Put the services in one file and the dataSource in the other. It worked fine. I changed the bean name to something else and the exception occurred.

                org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'example.MyTest': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.checkDependencies(Abstr actAutowireCapableBeanFactory.java:990)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:805)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:271)
                at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.injectDependencies(AbstractDe pendencyInjectionSpringContextTests.java:179)
                at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.prepareTestInstance(AbstractD ependencyInjectionSpringContextTests.java:158)
                at org.springframework.test.AbstractSingleSpringConte xtTests.setUp(AbstractSingleSpringContextTests.jav a:84)
                at junit.framework.TestCase.runBare(TestCase.java:125 )
                at org.springframework.test.ConditionalTestCase.runBa re(ConditionalTestCase.java:69)
                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)

                Comment


                • #9
                  So do you mean that my configurations files are ok? or do I need to change anything in my configuration files or AbstractTransactionalTest?

                  Comment


                  • #10
                    What I was getting at was I can't really see anything wrong with it. The only way I recreate the exception is to not have the bean present. Either change the name to be invalid or don't include the file that contains it. You're saying that both should be fine in your example, so I'm a little vexed. One thing you could try as a simple test. Create a class that extends AbstractTransactionalDataSourceSpringContextTests and include "mercury-data.xml" as the only file. See if that works ok.

                    Comment


                    • #11
                      One thing you could try as a simple test. Create a class that extends AbstractTransactionalDataSourceSpringContextTests and include "mercury-data.xml" as the only file. See if that works ok.
                      I will run this simple test and see whether it works.

                      Thanks alot
                      David

                      Comment


                      • #12
                        I am using weblogic8 as the application server and I have the server running when I run my Junit test. Is it something like this. or in other words how will including configuration file in AbstractTransactionalDataSourceSpringContextTests gets the dataSource?

                        Comment


                        • #13
                          There is a discussion here that might be useful.
                          http://forum.springframework.org/showthread.php?t=33323

                          Comment

                          Working...
                          X