【单元测试】如何编写spring dao的单元测试类、基于注解

1.框架结构

    spring +spring mvc+mybatis   idea

2.例子如下(仅供参考使用):


import com.raipeng.sales.entity.Sales;
import com.raipeng.sales.front.repository.SalesRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 111 on 2015/11/28.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager" ,defaultRollback = true)
public class SalesDaoTest {
    @Autowired
    public SalesRepository salesRepository;

    @Test
    public void testFindList(){
        List<Sales> salesList = salesRepository.findSalesListBySalesId(1l);
        System.out.println(salesList.size());
    }

    @Test
    @Rollback(value = false)
    public void testUpdateSales(){
        Sales sales = salesRepository.findSalesBySalesId(1l);
        sales.setCashStatus(1);
        sales.setAmount("9999.99");
        int num = salesRepository.updateSalesBySales(sales);
        System.out.println(num+"___________");
    }
} 

3.spring整合junit需要注意的是:

         spring只对junit4.5以上版本支持,否则@RunWith这个注解是无法引用的;

      

DAO编写单元测试,其中的如下<br>package com.javaeedev.dao.impl;<br><br>import java.util.List;<br>import java.util.UUID;<br><br>import com.javaeedev.dao.UserDao;<br>import com.javaeedev.domain.PasswordTicket;<br>import com.javaeedev.domain.User;<br>import com.javaeedev.exception.LockException;<br>import com.javaeedev.util.HibernateUtil;<br><br>public class UserDaoImpl implements UserDao {<br><br> public User queryForSignOn(String username) {<br> User user = queryUser(username);<br> if(user.getLocked())<br> throw new LockException(user.getLockDate());<br> return user;<br> }<br><br> public User queryUser(String username) {<br> return (User) HibernateUtil.query(User.class, username);<br> }<br><br> public void createUser(User user) {<br> user.setEmailValidation((int)(Math.random() * 1000000) + 0xf);<br> HibernateUtil.createEntity(user);<br> }<br><br> public void updateUser(User user) {<br> HibernateUtil.updateEntity(user);<br> }<br><br> public boolean updateEmailValidation(String username, int ticket) {<br> if(ticket==0)<br> return false;<br> return 1==HibernateUtil.executeUpdate(<br> "update User as u set u.emailValidation=0 where u.username=? and u.emailValidation=?",<br> new Object[] { username, ticket }<br> );<br> }<br><br> public String createPasswordTicket(User user) {<br> HibernateUtil.executeUpdate(<br> "delete from PasswordTicket as pt where pt.user=?",<br> new Object[] { user }<br> );<br> String ticket = UUID.randomUUID().toString().replaceAll("\\-", "");<br> PasswordTicket pt = new PasswordTicket();<br> pt.setUser(user);<br> pt.setTicket(ticket);<br> pt.setCreatedDate(System.currentTimeMillis());<br> HibernateUtil.createEntity(pt);<br> return ticket;<br> }<br><br> public boolean updatePassword(String username, String oldPassword, String newPassword) {<br> if(!newPassword.matches(User.REGEX_PASSWORD))<br> return false;<br> return 1==HibernateUtil.executeUpdate(<br> "update User as u set u.password=? where u.username=? and u.password=?",<br> new Object[] { newPassword, username, oldPassword }<br> );<br> }<br><br> public boolean queryResetPassword(User user, String ticket) {<br> long time = System.currentTimeMillis() - 48L * 3600000L;<br> return !HibernateUtil.queryForList(<br> "select pt from PasswordTicket as pt where pt.user=? and pt.ticket=? and pt.createdDate>?",<br> new Object[] { user, ticket, time}<br> ).isEmpty();<br> }<br><br> @SuppressWarnings("unchecked")<br> public boolean updateResetPassword(User user, String ticket, String password) {<br> if(!password.matches(User.REGEX_PASSWORD))<br> return false;<br> long time = System.currentTimeMillis() - 48L * 3600000L;<br> List<PasswordTicket> list = HibernateUtil.queryForList(<br> "select pt from PasswordTicket as pt where pt.user=? and pt.ticket=? and pt.createdDate>?",<br> new Object[] { user, ticket, time}<br> );<br> if(list.isEmpty())<br> return false;<br> HibernateUtil.executeUpdate(<br> "delete from PasswordTicket as pt where pt.user=?",<br> new Object[] { user }<br> );<br> HibernateUtil.executeUpdate(<br> "update User as u set u.password=? where u.username=?",<br> new Object[] { password, user.getUsername() }<br> );<br> return true;<br> }<br><br> public void updateLock(User user, long lockTime) {<br> HibernateUtil.executeUpdate(<br> "update User as u set u.lockDate=? where u.username=?",<br> new Object[] { System.currentTimeMillis() + lockTime, user.getUsername() }<br> );<br> }<br><br> public void updateUnlock(User user) {<br> HibernateUtil.executeUpdate(<br> "update User as u set u.lockDate=0 where u.username=?",<br> new Object[] { user.getUsername() }<br> );<br> }<br><br>}<br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值