Solved

junit test case

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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