码迷,mamicode.com
首页 > 其他好文 > 详细

关于JUnit的测试

时间:2014-05-26 18:40:52      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:c   class   java   ext   a   int   

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

    要测试的代码如下:

   

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的测试

标签:c   class   java   ext   a   int   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!