sudhakar14
asked on
junit test case
public class LoginDaoTest extends AbstractTransactionalDataS ourceSprin gContextTe sts {
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[]{"applicationConte xt.xml"};
}
/**
* Spring will automatically inject the Hibernate session factory on startup
* @param sessionFactory
*/
public void setSessionFactory(SessionF actory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void testAddCustomer(){
String query = "select count(*) from user where first_name = 'Firstname'";
int count = jdbcTemplate.queryForInt(q uery);
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("passwor d");
logindao.addCustomer(login );
// flush the session so we can get the record using JDBC template
SessionFactoryUtils.getSes sion(sessi onFactory, false).flush();
count = jdbcTemplate.queryForInt(q uery);
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.onSetUpBeforeTransac tion();
logindao = (LoginDAO)applicationConte xt.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.data source.Dri verManager DataSource ">
<property name="driverClassName"
value="com.mysql.jdbc.Driv er">
</property>
<property name="url"
value="jdbc:mysql://localh ost:3306/m ysql">
</property>
<property name="username" value="root"></property>
<property name="password" value="password"></propert y>
</bean>
<!-- <import resource="config/database/ spring/Hib ernateSess ionFactory .xml"/>-->
<bean id="sessionFactory"
class="org.springframework .orm.hiber nate3.anno tation.Ann otationSes sionFactor yBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.dialect">or g.hibernat e.dialect. MySQLDiale ct</prop>
<prop key="hibernate.show_sql">t rue</prop>
</props>
</property>
<!--
<property name="annotatedClasses">
<list>
<value>com.asc.Login</valu e>
<value>com.asc.Customer</v alue>
</list>
</property>
-->
</bean>
<bean name="logindao" class="com.asc.test.LoginD aoTest">
<property name="sessionFactory"><ref bean="sessionFactory"/> </property>
</bean>
<bean id="txManager" class="org.springframework .orm.hiber nate3.Hibe rnateTrans actionMana ger">
<property name="sessionFactory"><ref local="sessionFactory"/></ property>
</bean>
<!-- <bean name="loginDao" class="com.asc.test.LoginD aoTest">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>-->
<!-- Beans Declaration
<bean id="loginBO" class="com.asc.inter.impl. LoginBOImp l" >
<property name="logindao" ref="logindao" />
</bean>
<bean id="logindao" class="com.asc.inter.impl. LoginDAOIm pl" >
<property name="sessionFactory" ref="sessionFactory"></pro perty>
</bean>
<bean id="customerBo" class="com.asc.inter.impl. CustomerBO Impl" >
<property name="customerDao" ref="customerDao" />
</bean>
<bean id="customerDao" class="com.asc.inter.impl. CustomerDA OImpl" >
<property name="sessionFactory" ref="sessionFactory"></pro perty>
</bean>
-->
</beans>
i got this error
org.springframework.beans. factory.Un satisfiedD ependencyE xception: 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.su pport.Abst ractAutowi reCapableB eanFactory .checkDepe ndencies(A bstractAut owireCapab leBeanFact ory.java:9 23)
at org.springframework.beans. factory.su pport.Abst ractAutowi reCapableB eanFactory .populateB ean(Abstra ctAutowire CapableBea nFactory.j ava:728)
at org.springframework.beans. factory.su pport.Abst ractAutowi reCapableB eanFactory .autowireB eanPropert ies(Abstra ctAutowire CapableBea nFactory.j ava:266)
at org.springframework.test.A bstractDep endencyInj ectionSpri ngContextT ests.injec tDependenc ies(Abstra ctDependen cyInjectio nSpringCon textTests. java:205)
at org.springframework.test.A bstractDep endencyInj ectionSpri ngContextT ests.prepa reTestInst ance(Abstr actDepende ncyInjecti onSpringCo ntextTests .java:180)
at org.springframework.test.A bstractSin gleSpringC ontextTest s.setUp(Ab stractSing leSpringCo ntextTests .java:100)
at junit.framework.TestCase.r unBare(Tes tCase.java :125)
at org.springframework.test.C onditional TestCase.r unBare(Con ditionalTe stCase.jav a:76)
at junit.framework.TestResult $1.protect (TestResul t.java:106 )
at junit.framework.TestResult .runProtec ted(TestRe sult.java: 124)
at junit.framework.TestResult .run(TestR esult.java :109)
at junit.framework.TestCase.r un(TestCas e.java:118 )
at junit.framework.TestSuite. runTest(Te stSuite.ja va:208)
at junit.framework.TestSuite. run(TestSu ite.java:2 03)
at org.junit.internal.runners .JUnit38Cl assRunner. run(JUnit3 8ClassRunn er.java:83 )
at org.eclipse.jdt.internal.j unit4.runn er.JUnit4T estReferen ce.run(JUn it4TestRef erence.jav a:49)
at org.eclipse.jdt.internal.j unit.runne r.TestExec ution.run( TestExecut ion.java:3 8)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r unTests(Re moteTestRu nner.java: 467)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r unTests(Re moteTestRu nner.java: 683)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r un(RemoteT estRunner. java:390)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.m ain(Remote TestRunner .java:197)
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[]{"applicationConte
}
/**
* Spring will automatically inject the Hibernate session factory on startup
* @param sessionFactory
*/
public void setSessionFactory(SessionF
this.sessionFactory = sessionFactory;
}
public void testAddCustomer(){
String query = "select count(*) from user where first_name = 'Firstname'";
int count = jdbcTemplate.queryForInt(q
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("passwor
logindao.addCustomer(login
// flush the session so we can get the record using JDBC template
SessionFactoryUtils.getSes
count = jdbcTemplate.queryForInt(q
assertEquals("User was not found in the DB", 1, count);
}
/**
* Overridden method from base class which gets called automatically
*/
protected void onSetUpBeforeTransaction()
super.onSetUpBeforeTransac
logindao = (LoginDAO)applicationConte
}
}
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
<property name="driverClassName"
value="com.mysql.jdbc.Driv
</property>
<property name="url"
value="jdbc:mysql://localh
</property>
<property name="username" value="root"></property>
<property name="password" value="password"></propert
</bean>
<!-- <import resource="config/database/
<bean id="sessionFactory"
class="org.springframework
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties"
<props>
<prop key="hibernate.dialect">or
<prop key="hibernate.show_sql">t
</props>
</property>
<!--
<property name="annotatedClasses">
<list>
<value>com.asc.Login</valu
<value>com.asc.Customer</v
</list>
</property>
-->
</bean>
<bean name="logindao" class="com.asc.test.LoginD
<property name="sessionFactory"><ref
</bean>
<bean id="txManager" class="org.springframework
<property name="sessionFactory"><ref
</bean>
<!-- <bean name="loginDao" class="com.asc.test.LoginD
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>-->
<!-- Beans Declaration
<bean id="loginBO" class="com.asc.inter.impl.
<property name="logindao" ref="logindao" />
</bean>
<bean id="logindao" class="com.asc.inter.impl.
<property name="sessionFactory" ref="sessionFactory"></pro
</bean>
<bean id="customerBo" class="com.asc.inter.impl.
<property name="customerDao" ref="customerDao" />
</bean>
<bean id="customerDao" class="com.asc.inter.impl.
<property name="sessionFactory" ref="sessionFactory"></pro
</bean>
-->
</beans>
i got this error
org.springframework.beans.
at org.springframework.beans.
at org.springframework.beans.
at org.springframework.beans.
at org.springframework.test.A
at org.springframework.test.A
at org.springframework.test.A
at junit.framework.TestCase.r
at org.springframework.test.C
at junit.framework.TestResult
at junit.framework.TestResult
at junit.framework.TestResult
at junit.framework.TestCase.r
at junit.framework.TestSuite.
at junit.framework.TestSuite.
at org.junit.internal.runners
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.