Solved

junit test case

Posted on 2010-11-17
1
742 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to view directory list in apache/tomcat 9 115
Lamba in java 8 9 242
excpetion with multiple catch 11 116
spring example non maven 4 87
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 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