51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2496|回复: 1
打印 上一主题 下一主题

求教,急!非常急

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2005-7-28 08:50:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近公司需要我们测试这边进行单元测试(代码级测试),但是不会用JUNIT,我看了从论坛上一些有关JUNIT的资料,但是还是不清楚。我们用的是JAVA开发,开发工具是JBUILDER9,所以希望各位大大帮帮忙教我一下,下面有个例子。希望各位大大能按例子讲解,讲解一下怎么单元测试,同时希望对某些关键部分加上注释。特别是suite(),tearDown()完全不知道怎么用,只知道suite()方法是用来执行测试,tearDown()是释放资源用,具体怎么用非常的不清楚。

package com.test.mbo.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import com.test.mbo.MBOLogger;
import com.test.mbo.exception.*;

public class TranInfoDao
{
  private Connection conn;

  public TranInfoDao(Connection conn)
  {
    this.conn = conn;
  }

  public void update(TranInfo tranInfo)
    throws SQLException, NoSuchEntityException
  {
    MBOLogger.info("更新对象的实例 " + tranInfo);

    PreparedStatement ps = null;
    ResultSet rs = null;

    StringBuffer sb = new StringBuffer();
    sb.append("UPDATE TABLE_TRANLOG SET ");
    sb.append("REP_CODE=? ");
    sb.append("WHERE TRAN_LOGRECID=?");

    try {

      MBOLogger.debug("Preparing statement.");
      MBOLogger.debug(sb.toString());

      ps = conn.prepareStatement(sb.toString());
      ps.setString(1, tranInfo.getRepCode());
      ps.setString(2, tranInfo.getTranNo());
      if (ps.executeUpdate() != 1)
      {
        throw new NoSuchEntityException("没有发现相应的记录可以更新");
      }
    }
    catch (SQLException e)
    {
      throw new SQLException(e.getMessage());
    }
    finally
    {
      DAOFactory.cleanup(rs, ps);
    }
  }

  /**
   * 生成对帐文件
   */
  public void produceTranFile(java.util.Date lastTime, java.util.Date now, OutputStream os)
     throws SQLException, IOException
  {
    MBOLogger.info("生成从" + lastTime + "到" + now + "的对帐文件");

    PreparedStatement ps = null;
    ResultSet rs = null;
    TranInfo tranInfo = null;

    StringBuffer sb = new StringBuffer();
    sb.append("SELECT ");
    sb.append("TRAN_CODE,");     
    sb.append("PROC_CODE,");     
    sb.append("MSG_SN,");        
    sb.append("TRAN_TIME,");     
    sb.append("TRAN_DATE,");     
    sb.append("REF_NO,");        
    sb.append("TERMINAL_ID,");   
    sb.append("TRAN_AMT,");      
    sb.append("REP_CODE ");      
    sb.append("FROM TABLE_TRANLOG ");
    sb.append("WHERE SYS_DATE>? AND SYS_DATE<=? ");
    try
    {
      ps = conn.prepareStatement(sb.toString());

      ps.setTimestamp(1, new java.sql.Timestamp(lastTime.getTime()));
      ps.setTimestamp(2, new java.sql.Timestamp(now.getTime()));

      rs = ps.executeQuery();
      int i = 0;
      DataOutputStream dos = new DataOutputStream(os);
      while(rs.next())
      {
        i++;
        StringBuffer buf = new StringBuffer();
        buf.append(rs.getString(1) + ";");
        buf.append(rs.getString(2) + ";");
        buf.append(rs.getString(3) + ";");
        buf.append(rs.getString(4) + ";");
        buf.append(rs.getString(5) + ";");
        buf.append(rs.getString(6) + ";");
        buf.append(rs.getString(7) + ";");
        buf.append(rs.getString(8) + ";");
        buf.append(rs.getString(9));
        dos.writeBytes(buf.toString());
        dos.write(0x0d);
        dos.write(0x0a);
      }
      dos.close();

    }
    catch (SQLException e)
    {
      throw new SQLException("网络连接失败");
    }
    catch (IOException ioe)
    {
      throw ioe;
    }
    finally
    {
      DAOFactory.cleanup(rs, ps);
    }
  }

  public TranInfo find(String tranNo)
     throws FinderException, SQLException
  {
    MBOLogger.info("根据关键字查询: " + tranNo);

    PreparedStatement ps = null;
    ResultSet rs = null;
    TranInfo tranInfo = null;

    StringBuffer sb = new StringBuffer();
    sb.append("SELECT ");
    sb.append("ACCOUNT,");
    sb.append("TRAN_AMT,");
    sb.append("TRAN_TIME,");
    sb.append("TRAN_DATE,");
    sb.append("REF_NO,");
    sb.append("MSG_SN,");
    sb.append("TERMINAL_ID ");
    sb.append("FROM TABLE_TRANLOG WHERE TRAN_LOGRECID=?");
    try
    {
      MBOLogger.debug("Preparing statement.");
      MBOLogger.debug(sb.toString());
      ps = conn.prepareStatement(sb.toString());
      ps.setString(1, tranNo);
      rs = ps.executeQuery();
      MBOLogger.debug("Executed query.");
      if (rs.next()) {
        tranInfo = new TranInfo();
        tranInfo.setAccount(rs.getString(1));
        tranInfo.setTranAmt(rs.getDouble(2));
        tranInfo.setTranTime(rs.getString(3));
        tranInfo.setTranDate(rs.getString(4));
        tranInfo.setRefNo(rs.getString(5));
        tranInfo.setMsgSn(rs.getString(6));
        tranInfo.setTerminalID(rs.getString(7));
        return tranInfo;
      }
      else
      {
        //throw new ObjectNotFoundException("没有找到合适的记录");
        return null;
      }
    } catch (SQLException e) {
      throw new SQLException("网络连接失败");

    } finally {
      DAOFactory.cleanup(rs, ps);
    }
  }

  public void Create(TranInfo tranInfo)
    throws CreateException, SQLException
  {
                MBOLogger.info("创建一条新的记录: " + tranInfo);
    PreparedStatement ps = null;

    StringBuffer str = new StringBuffer();
    str.append("INSERT INTO TABLE_TRANLOG ");
    str.append(" (TRAN_LOGRECID, SETTLE_DATE, TRAN_CODE, PROC_CODE, MSG_SN, ");
    str.append(" TRAN_DATE, TRAN_TIME, TRAN_AMT, REF_NO, ORIG_SN, REP_CODE, ");
    str.append(" MSG_SOURCE, SYSTEM_SN, DO_FLAG, ACCOUNT, TERMINAL_ID, SYS_DATE)");
    str.append(" VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate)");

    try
    {
      if (conn.isClosed())
      {
        throw new IllegalStateException("数据库连接已经关闭!!!");
      }

      MBOLogger.debug("Preparing statement.");
                        MBOLogger.debug(str.toString());

      ps = conn.prepareStatement(str.toString());

      ps.setString(1, tranInfo.getTranNo());
      ps.setString(2, tranInfo.getSettleDate());
      ps.setString(3, tranInfo.getTranCode());
      ps.setString(4, tranInfo.getProcCode());
      ps.setString(5, tranInfo.getMsgSn());
      ps.setString(6, tranInfo.getTranDate());
      ps.setString(7, tranInfo.getTranTime());
      ps.setDouble(8, tranInfo.getTranAmt());
      ps.setString(9, tranInfo.getRefNo());
      ps.setString(10, tranInfo.getOrigNo());
      ps.setString(11, tranInfo.getRepCode());
      ps.setString(12, tranInfo.getMsgSource());
      ps.setString(13, tranInfo.getSystemSn());
      ps.setString(14, tranInfo.getDoFlag());
      ps.setString(15, tranInfo.getAccount());
      ps.setString(16, tranInfo.getTerminalID());


      if (ps.executeUpdate() != 1)
      {
        throw new CreateException("数据库有相同的记录");
      }
    }
    catch(SQLException sqle)
    {
                        throw new SQLException(sqle.getMessage());
    }
    finally
    {
                        DAOFactory.cleanup(null, ps);
    }




测试代码(总是不能编译)

package com.test.mbo;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import junit.framework.*;
import com.test.mbo.dao.TranInfoDao;
import com.test.mbo.dao.TranInfo;
import java.sql.Statement;
import java.util.Properties;
import java.sql.DriverManager;
import com.test.mbo.exception.FinderException;

/**
* <p>Title: test</p>
* <p>Description: test</p>
* <p>Copyright: Copyright (c) test 2004</p>
* <p>Company: test</p>
* @author not attributable
* @version 1.0
*/

public class testTranlnfoDao
    extends TestCase {
  private String data;
  private TranInfoDao testinfodao;
  private TranInfo testinfo;
  private Connection testconn = null;
  private static Test suite;
  
public testTranlnfoDao(String name) {
    super(name);
  }

  public static Test suite()
  {
  TestSuite suite= new TestSuite();
  suite.addTest(runTest());
  return suite;
  }

  protected void runTest() throws FinderException, SQLException {
    TestUpdate();
  }
  protected void setup();

  {
    testinfo = new com.test.mbo.dao.TranInfo();
    testinfodao = new com.test.mbo.dao.TranInfoDao(testconn);
  }

  public static Connection getConnection() throws SQLException {

    String url ="jdbc.url";
    String username = "jdbc.username";
    String password ="jdbc.password";
    return DriverManager.getConnection(url, username, password);
  }

  public void TestUpdate() throws SQLException, FinderException
  {
    Connection testcoon = getConnection();
    testinfo.setRepCode("test1");
    testinfo.setTranNo("46");
    testinfodao.update(testinfo);
    TranInfo testinfoResult = null;
    testinfoResult = testinfodao.find("46");
    assertEquals("test1", testinfoResult.getRefNo());
    if (testinfoResult == null);
    throw new  SQLException();
   }

  public void testtearDown() throws SQLException {
    String datacommand = "DELETE FORM TABLE_TRANLOG" +
        "WHERE TRAN_LOGRECID=(test1)";
    Statement datastat = testconn.createStatement();
    datastat.executeUpdate(datacommand);
    testconn.close();
  }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2005-8-12 12:10:08 | 只看该作者
建议如果没有设计文档,只有代码,还是别做单元测试的好,把时间省下来走读代码发现问题的效率更高。
单元测试不是这么个做法。
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-14 11:06 , Processed in 0.072785 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表