Solved

junit test case

Posted on 2010-11-17
1
736 Views
Last Modified: 2012-05-10
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)

0
Comment
Question by:sudhakar14
1 Comment
 
LVL 9

Accepted Solution

by:
ioanton earned 500 total points
ID: 34153469
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
decryption not working 5 81
Apache server configuration 7 79
excpetion with multiple catch 11 105
struts exact match support 3 45
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
A Short Story about the Best File Recovery Software – Acronis True Image 2017
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now