junit4 - writing Junit test case for DAO -
i'm new using mybatis3 developing web application. have dao impl class , related mapper.xml . want write junit case dao.if able me, extremely grateful.
here's have:
customer-mapper.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="customer"> <insert id="newcustomer" parametertype="com.yell.hibu.domain.customer"> insert customer(customer_id, name, email_id,graphic_name,mobile,dob,language,city,country) values(#{customerid},#{customername},#{emailid},#{userfilefilename},#{mobileno},#{dob},#{language},#{city},#{country}) </insert> <select id="selectcustomer" resultmap="customer"> select * customer customer_id = #{customerid}; </select> <resultmap type="com.yell.hibu.domain.customer" id="customer"> <result property="customerid" column="customer_id" /> <result property="customername" column="name" /> <result property="emailid" column="email_id" /> <result property="userfilefilename" column="graphic_name" /> <result property="mobileno" column="mobile" /> <result property="dob" column="dob" /> <result property="language" column="language" /> <result property="city" column="city" /> <result property="country" column="country" /> </resultmap> </mapper>
daoimpl.java
package com.yell.hibu.dao; import org.apache.log4j.logger; import org.mybatis.spring.support.sqlsessiondaosupport; import com.yell.hibu.domain.customer; public class customerdaoimpl extends sqlsessiondaosupport implements customerdao{ private final logger logger= logger.getlogger(this.getclass()); public void insertcustomer(customer customer) { logger.info("entered customerdaoimpl :: insertcustomer()"); getsqlsession().insert("customer.newcustomer", customer); logger.info("leaving customerdaoimpl :: insertcustomer()"); } }
since use easymock, mock sessionfactory instance returns mocked session object, or throws exception.
@test(expectedexceptions = exception.class) public void testfailuretoconnect() throws exception { iocsessionfactory = easymock.createmock(sqlsessionfactory.class); oracledaoimpl.setiocoraclesessionfactory(iocsessionfactory); easymock.expect(iocsessionfactory.opensession()).andthrow(new runtimeexception("connection error")); easymock.replay(iocsessionfactory); oracledaoimpl.getdata(); }
or if don't want mock everything, check out https://code.google.com/p/mybatis/wiki/test
Comments
Post a Comment