Link to home
Start Free TrialLog in
Avatar of sudhakar14
sudhakar14

asked on

junit test case

public class LoginDaoTest extends AbstractTransactionalDataSourceSpringContextTests {
      private LoginDAO logindao;
 

      public LoginDAO getLogindao() {
            return logindao;
      }
      public void setLogindao(LoginDAO logindao) {
            this.logindao = logindao;
      }

      private SessionFactory sessionFactory = null;
        protected String[] getConfigLocations() {
            return new String[]{"applicationContext.xml"};
        }
      
        /**
         * Spring will automatically inject the Hibernate session factory on startup
         * @param sessionFactory
         */
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
        public void testAddCustomer(){
            String query = "select count(*) from user where first_name = 'Firstname'";
            int count = jdbcTemplate.queryForInt(query);
            assertEquals("A user already exists in the DB", 0, count);
          
            Login login = new Login();
            login.setFname("cfdd");
            login.setLname("lname");
            login.setUserName("krishna");
            login.setPassword("password");
          
            logindao.addCustomer(login);
          
            // flush the session so we can get the record using JDBC template
            SessionFactoryUtils.getSession(sessionFactory, false).flush();
          
            count = jdbcTemplate.queryForInt(query);
            assertEquals("User was not found in the DB", 1, count);
        }
        
        /**
         * Overridden method from base class which gets called automatically
         */
        protected void onSetUpBeforeTransaction() throws Exception {
            super.onSetUpBeforeTransaction();
            logindao = (LoginDAO)applicationContext.getBean("logindao");
              }
      }
and

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
      <!-- Database Configuration-->

      
       <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"
                  value="com.mysql.jdbc.Driver">
            </property>
            <property name="url"
                  value="jdbc:mysql://localhost:3306/mysql">
            </property>
            <property name="username" value="root"></property>
            <property name="password" value="password"></property>
      </bean>

      
      
 

      <!-- <import resource="config/database/spring/HibernateSessionFactory.xml"/>-->
 <bean id="sessionFactory"
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 
    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>
 
    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>
 <!--
    <property name="annotatedClasses">
            <list>
                 <value>com.asc.Login</value>
                  <value>com.asc.Customer</value>
            </list>
     </property>      
   -->    
 
</bean>
       <bean name="logindao" class="com.asc.test.LoginDaoTest">
       <property name="sessionFactory"><ref bean="sessionFactory"/> </property>
    </bean>
   <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
   <!--    <bean name="loginDao" class="com.asc.test.LoginDaoTest">
       <property name="sessionFactory">
           <ref bean="sessionFactory"/>
       </property>
    </bean>-->
      <!-- Beans Declaration
      
         <bean id="loginBO" class="com.asc.inter.impl.LoginBOImpl" >
               <property name="logindao" ref="logindao" />
         </bean>
 
         <bean id="logindao" class="com.asc.inter.impl.LoginDAOImpl" >
               <property name="sessionFactory" ref="sessionFactory"></property>
         </bean>
               <bean id="customerBo" class="com.asc.inter.impl.CustomerBOImpl" >
               <property name="customerDao" ref="customerDao" />
         </bean>
 
         <bean id="customerDao" class="com.asc.inter.impl.CustomerDAOImpl" >
               <property name="sessionFactory" ref="sessionFactory"></property>
         </bean>
 -->
</beans>


i got this error

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.asc.test.LoginDaoTest': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:923)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:728)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:266)
      at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:205)
      at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:180)
      at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:100)
      at junit.framework.TestCase.runBare(TestCase.java:125)
      at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
      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.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

ASKER CERTIFIED SOLUTION
Avatar of ioanton
ioanton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial