spring mvc - How to work with EasyMock for testing servicelayer in SpringMVC? -
i working on hibernate , spring mvc application,but want test servicelayer using easy mock ,i doing not understanding correct way or not.please check code below , give me suggestions
studentdao
package com.bhanu.dao; import com.bhanu.entity.studententity; import com.bhanu.modelpojo.student; public interface studentdao { public studententity login(student student); public studententity finduser(string email);
} studentservice
package com.bhanu.service; import com.bhanu.entity.studententity; import com.bhanu.modelpojo.student; public interface studentservice{ public studententity login(student student); public studententity finduser(string email);
}
studentserviceimpl
@service public class studentserviceimpl implements studentservice { @autowired private basedao basedao; @autowired private javamailsender mailsender; @autowired private studentdao studentdao; public studentserviceimpl() { super(); // todo auto-generated constructor stub } @override public studententity login(student student) { studententity studententity = studentdao.login(student); return studententity; } public basedao getbasedao() { return basedao; } public void setbasedao(basedao basedao) { this.basedao = basedao; } public javamailsender getmailsender() { return mailsender; } public void setmailsender(javamailsender mailsender) { this.mailsender = mailsender; } public studentdao getstudentdao() { return studentdao; } public void setstudentdao(studentdao studentdao) { this.studentdao = studentdao; } }
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.bhanu" /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/web-inf/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="url" value="jdbc:mysql://localhost:3306/student" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="packagestoscan" value="com.bhanu.entity" /> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysql5dialect</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.cache.use_second_level_cache">false</prop> <prop key="hibernate.cache.use_query_cache">false</prop> </props> </property> </bean> <bean id="hibernatetemplate" class="org.springframework.orm.hibernate3.hibernatetemplate"> <property name="sessionfactory" ref="sessionfactory" /> </bean>
mytestcase
package com.bhanu.servicesimpl; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath:mvc-dispatcher-servlet.xml") public class mytestcase { private studentserviceimpl studentserviceimpl; private studentdao studentdao; private basedao basedao; private javamailsender javamailsender; //execute before test @before public void before() { studentdao =easymock.createstrictmock(studentdao.class); basedao =easymock.createstrictmock(basedao.class); javamailsender =easymock.createstrictmock(javamailsender.class); studentserviceimpl = new studentserviceimpl(); studentserviceimpl.setbasedao(basedao); studentserviceimpl.setmailsender(javamailsender); studentserviceimpl.setstudentdao(studentdao); system.out.println("in before"); } //test case @test public void test() { system.out.println("in test"); student student = new student(); student.setemail("hari@gmail.com"); student.setpassword("12530"); easymock.expect(studentdao.login(student)) .andreturn(new studententity()); easymock.replay(studentdao); studententity studententity1 =studentserviceimpl.login(student); system.out.println(studententity1 .getemailid());***//i got null value*** assertnotnull(studententity1); easymock.verify(studentdao); } }
for more information writing daoimpl studentdaoimpl
@suppresswarnings("unchecked") @repository public class studentdaoimpl implements studentdao { @autowired private hibernatetemplate hibernatetemplate; @override public studententity finduser(string email) { studententity mystudentobject = null; studententity studententity = new studententity(); studententity.setemail(email); list<studententity> studentlist = hibernatetemplate.findbyexample( studententity.getclass().getname(), studententity); if (studentlist != null && studentlist.isempty() && studentlist.size()!=0) { mystudentobject = dataaccessutils.requireduniqueresult(studentlist); } return mystudentobject; }
}
in database have record hari@gmail.com & above password , not getting record when exectue testcase .please tell me if doing thing wrong , correct way write easy mock,if wrong please tell me how write testcase using easy mock .any 1 me.
what have normal. think haven't understood mock is. mock "false" implementation of class or interface nothing, except tell do. whole point of mocking daos when testing services able test service logic without needing functional dao, database populated test data, etc.
so when you're saying
studentdao =easymock.createstrictmock(studentdao.class); studentserviceimpl = new studentserviceimpl(); studentserviceimpl.setstudentdao(studentdao);
you're creating service uses such "false" dao.
by default, dao methods won't except return null or default value.
when do
easymock.expect(studentdao.login(student)).andreturn(new studententity());
and replay mock, every time dao's login() method called student argument, return new studententity. , obviously, new studententity has null email, since constructor of studententity leaves email null. have in database irrelevant.
note such test doesn't need spring context run. doesn't need annotated
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath:mvc-dispatcher-servlet.xml")
since you're not using spring @ all. that's why dependency injection useful: allows unit-testing code without needing dependency, framework or anything.
Comments
Post a Comment