Solved

junit test case

Posted on 2010-11-17
1
748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
ioanton earned 500 total points
ID: 34153469
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

We asked our MSP customer base what their favorite tools were and how they help them serve clients. We focused our questions on favorite tools in the following categories: >PSA tools >RMM tools >Alert management tools >Communication tools and Mo…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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