首页 > 其他 > 详细

关于JUnit的测试

时间:2014-05-26 18:41:02      阅读:362      评论:0      收藏:0      [点我收藏+]

    把以前的笔记整理一下,做个备份方便以后查阅:

    要测试的代码如下:

   

package dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import util.DBUtil;

import dao.IAccountDAO;
import entity.Account;

public class AccountDAOImpl implements IAccountDAO{

 @Override
 public Account findByAccountNo(
   String accountNo) throws Exception {
  Account a = null;
  Connection conn = null;
  PreparedStatement stat = null;
  ResultSet rst = null;
  try{
   conn = DBUtil.getConnection();
   String sql = "select * from t_account " +
     "where accountNo=?";
   stat = conn.prepareStatement(sql);
   stat.setString(1, accountNo);
   rst = stat.executeQuery();
   if(rst.next()){
    a = new Account();
    a.setAccountNo(accountNo);
    a.setBalance(rst.getInt("balance"));
    a.setId(rst.getInt("id"));
   }
  }catch(Exception e){
   e.printStackTrace();
   throw e;
  }finally{
   if(rst != null){
    rst.close();
   }
   if(stat != null){
    stat.close();
   }
   DBUtil.close(conn);
  }
  return a;
 }

 @Override
 public void modify(Account a) throws Exception {
  Connection conn = null;
  PreparedStatement stat = null;
  try{
   conn = DBUtil.getConnection();
   String sql = "update t_account set " +
     "balance=? where accountNo=?";
   stat = conn.prepareStatement(sql);
   stat.setInt(1, a.getBalance());
   stat.setString(2, a.getAccountNo());
   stat.executeUpdate();
  }catch(Exception e){
   e.printStackTrace();
   throw e;
  }finally{
   if(stat != null){
    stat.close();
   }
   DBUtil.close(conn);
  } 
 }
}

测试代码:

package dao.jdbc;

import org.junit.Test;

import util.Factory;

import dao.IAccountDAO;
import entity.Account;

public class AccountDAOImplTest {

 @Test
 public void testFindByAccountNo() throws Exception {
  IAccountDAO dao =
   (IAccountDAO) Factory.getInstance(
     "IAccountDAO");
  Account a = dao.findByAccountNo(
    "6225881003192000");
  System.out.println(a);
 }
 
 @Test
 public void testModify() throws Exception{
  IAccountDAO dao =
   (IAccountDAO) Factory.getInstance(
     "IAccountDAO");
  Account a = dao.findByAccountNo(
    "6225881003192000");
  a.setBalance(a.getBalance() - 800);
  dao.modify(a);
 }

}

关于JUnit的测试,布布扣,bubuko.com

关于JUnit的测试

原文:http://www.cnblogs.com/mxyhws/p/3752409.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!