JUnit in java
單元測試配置:
選擇你要測試類中的方法,點擊完成!便生成測試類的基本框架,如下代碼,我們以對一個DAO類測試為例:
/*
* Copyright reserved 2005 by XXXXCo. Ltd.
* Author:Fu wei Date:2006-9-4
*/
import junit.framework.TestCase;
/**
* @author Fu wei
*/
public class OrgTypeDAOTest extends TestCase
{/**
* @param arg0
*/
public OrgTypeDAOTest(String arg0)
{super(arg0);
}
/*
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{super.setUp();
}
/*
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception
{super.tearDown();
}
/**
* 主函數
* @param args
*/
public static void main(String[] args)
TestRunner.run(OrgTypeDAOTest . {class);
}
/**
* {@link OrgTypeDAO#getOrgTypeList()} 的測試方法。
*/
public final void testGetOrgTypeList()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#insertOrgTypeInfo(com.zhjy.mltx.vo.OrgTypeVO)} 的測試方法。
*/
public final void testInsertOrgTypeInfo()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#deleteOrgTypeInfo(java.lang.String)} 的測試方法。
*/
public final void testDeleteOrgTypeInfo()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#updateOrgTypeInfo(com.zhjy.mltx.vo.OrgTypeVO)} 的測試方法。
*/
public final void testUpdateOrgTypeInfo()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#getOrgTypeInfoById(java.lang.String)} 的測試方法。
*/
public final void testGetOrgTypeInfoById()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#isRepeatOrgTypeInfo(java.lang.String)} 的測試方法。
*/
public final void testIsRepeatOrgTypeInfoString()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#isRepeatOrgTypeInfo(com.zhjy.mltx.vo.OrgTypeVO)} 的測試方法。
*/
public final void testIsRepeatOrgTypeInfoOrgTypeVO()
fail(" {尚未實現"); // TODO
}
/**
* {@link OrgTypeDAO#getFlatOrgIdByName(java.lang.String)} 的測試方法。
*/
public final void testGetFlatOrgIdByName()
fail(" {尚未實現"); // TODO
}
}
JUnit單元測試一共要注意一下幾點:
(1)import junit.framework.TestCase 和 junit.textui.TestRunner;
(2)繼承junit.framework.TestCase ;
(3)自行添加一個main方法 中調用TestRunner.run(測試類名.class);
(4)有一個調用super(String)的構造函數;
以上都是JUnit必有的特征,除以上外,我們發現有許多以test開頭的方法,而這些方法正是我們要測試的方法,Junti測試其實采用的是斷言的方式,只要我們在所有test開頭中的方法對數據添加斷言方法,同時提供很多斷言的方法,
常用斷言方法 |
|
assertEquals("失敗提示信息","期望數據","測試數據") |
斷言獲取數據是否與所期望的相等 |
assertNotNull("失敗提示信息","測試數據") |
斷言獲取數據不為null,否則提示錯誤 |
assertNull("失敗提示信息","測試數據") |
斷言獲取數據是為null,否則提示錯誤 |
assertTrue("失敗提示信息",測試數據blooean值) |
斷言獲取數據是否為ture,否則提示錯誤 |
fail("失敗提示信息"); |
此方法一般放到異常處,遇到此方法,測試將停止! |
assertSame("失敗提示信息","期望數據","測試數據") |
斷言獲取數據是否與所期望的相同 |
當我們寫完所有方法策略后,JUnit測試如下圖:
在方法頁面中點擊右鍵在“調試方式”或“運行方式”中點擊“JUnit 測試”,就運行測試類!
在執行測試類時,執行的大概過程:
(1)先執行構造方法public OrgTypeDAOTest(String arg0) ;
(2)再執行初始化數據方法protected void setUp() ;
(3)再執行以test開頭的測試方法;
(4)最后執行protected void tearDown()方法清理對象;
如果測試失敗或者錯誤,將會顯示一個紅色的亮條;如果測試通過將顯示綠色亮條;如下圖
這樣就把一個整個單元測試操作例子演示完成!
可能對于一個測試類中有多個方法要測試,對于后面看著的確有些困難,因此,我對上測試類進行簡單的調整,如下代碼:
import junit.framework.TestCase;
import junit.textui.TestRunner;
import com.zhjy.mock.SpringMock;
/**
* 舉例測試類
*/
public class OrgTypeDAOTest extends TestCase { //(1)繼承TestCase
//private OrgTypeDAO orgTypeDAO;
//private OrgTypeVO orgTypeVO;
//private String id ;
/**
* 構造方法
* @param arg0
*/
public OrgTypeDAOTest(String arg0)
{super(arg0);
}
/* 初時化方法
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{super.setUp();
//測試初始話數據調用類 orgTypeDAO和 封裝數據的對象orgTypeVO
}
/* 執行完清理方法
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception
{//清空 對象 ;==null
//orgTypeDAO =null;
//orgTypeVO =null;
super.tearDown();
}
/**
* 主函數
* @param args
*/
public static void main(String[] args)
TestRunner.run(OrgTypeDAOTest. {class);
}
/**
* 測試方法
* Test method testOrgTypeInfo
*/
public void testOrgTypeInfo()
{//添加
String id = insertOrgTypeInfo();
//列表
orgTypeList();
//修改
updateOrgTypeInfo(id);
//查詢
selectOrgTypeInfoById(id);
//校驗
iExistOrgByOrgTypeId(id);
//測試是否重復數據方法(add)
isRepeatOrgTypeInfo(orgTypeVO.getName(),"");
//獲取數據方法(根據名稱)
selectOrgTypeIdByName(orgTypeVO.getName());
//刪除
deleteOrgTypeInfo(id);
}
/*
*添加初始數據
*/
private void setOrgTypeVOAddInfo()
orgTypeVO.setName("add {中海測試");
orgTypeVO.setDescription("add中海測試");
orgTypeVO.setStatus("1");
}
/*
*添加初始數據
*/
private void setOrgTypeVOUpdateInfo()
{//orgTypeVO.setId(id);
orgTypeVO.setName("add中海測試");
orgTypeVO.setDescription("update中海測試");
orgTypeVO.setStatus("1");
}
/*
* 新增方法
* Test method for {@link OrgTypeDAO#insertOrgTypeInfo(com.zhjy.mltx.vo.OrgTypeVO)}.
*/
public String insertOrgTypeInfo()
setOrgTypeVOAddInfo();
String id = {null;
try{
id = orgTypeDAO.insertOrgTypeInfo(orgTypeVO);
}catch(Exception e)
fail(" {添加通用組織機構失敗!");
}
return id;
}
/*
* 更新方法
* Test method for {@link com.zhjy.mltx.dao.OrgTypeDAO#updateOrgTypeInfo(com.zhjy.mltx.vo.OrgTypeVO)}.
*/
public void updateOrgTypeInfo(String id)
setOrgTypeVOUpdateInfo();
orgTypeVO.setId(id);
{try{
orgTypeDAO.updateOrgTypeInfo(orgTypeVO);
}catch(Exception e)
assertTrue(" {修改通用組織機構失敗!", false);
}
//查詢
orgTypeVO = orgTypeDAO.selectOrgTypeInfoById(id);
assertEquals("修改通用組織機構失敗!", orgTypeVO.getDescription(), "update中海測試");
}
/*
* 獲取數據方法(主健)
* Test method for {@link OrgTypeDAO#selectOrgTypeInfoById(java.lang.String)}.
*/
public void selectOrgTypeInfoById(String id)
orgTypeVO = orgTypeDAO.selectOrgTypeInfoById(id);
assertTrue(" {無法查看一條通用機構名稱信息!",orgTypeVO != null);
assertEquals("添加通用組織機構失敗!", orgTypeVO.getName(), "add中海測試");
assertEquals("修改通用組織機構失敗!", orgTypeVO.getDescription(), "update中海測試");
}
/*
* 測試此通用組織機構是否被引用
* Test method for {@link OrgTypeDAO#iExistOrgByOrgTypeId(java.lang.String)}.
*/
public void iExistOrgByOrgTypeId(String id)
{boolean isfalse;
try
isfalse = {this.orgTypeDAO.iExistOrgByOrgTypeId(id);
assertFalse("通用組織機構校驗錯誤!",isfalse);
} catch (DataAccessException e)
assertTrue(" {通用組織機構數據操作錯誤!!",false);
} catch (ObjectNotFoundException e)
assertTrue("id is null!!", {false);
}
}
/*
* 刪除
* Test method for {@link OrgTypeDAO#deleteOrgTypeInfo(java.lang.String)}.
*/
public void deleteOrgTypeInfo(String id)
{this.orgTypeDAO.deleteOrgTypeInfo(id);
orgTypeVO = this.orgTypeDAO.selectOrgTypeInfoById(id);
assertNull("刪除通用組織機構失敗!", orgTypeVO);
}
/*
* 獲取數據方法(根據名稱)
* Test method for {@link OrgTypeDAO#selectOrgTypeIdByName(java.lang.String)}.
*/
public void selectOrgTypeIdByName(String name)
String orgtypeid = orgTypeDAO.selectOrgTypeIdByName(name);
{//assertEquals(orgtypeid, id);
assertNotNull("未查出來通用組織機構ID!",orgtypeid);
}
/*
* 測試是否重復數據方法
* Test method for {@link OrgTypeDAO#isRepeatOrgTypeInfo(com.zhjy.mltx.vo.OrgTypeVO)}.
*/
public void isRepeatOrgTypeInfo(String name,String id)
{//setOrgTypeVOUpdateInfo();
OrgTypeVO orgtypetest = new OrgTypeVO();
orgtypetest.setId(id);
orgtypetest.setName(name);
boolean isTrue = orgTypeDAO.isRepeatOrgTypeInfo(orgtypetest);
//assertEquals("通用組織機構錯誤數據",isTrue, false);
assertTrue("通用組織機構錯誤數據", isTrue);
}
/*
* 列表方法
* Test method for {@link com.zhjy.mltx.dao.OrgTypeDAO#orgTypeList()}.
*/
public void orgTypeList()
List list = orgTypeDAO.orgTypeList();
assertNotNull(" {無法獲取通用機構名稱列表list is null!",list);
}
}
處理過程:
(1)把所有以test開頭的方法中的方法名稱前的test去掉(例如把testOrgTypeList()改為orgTypeList()方法);
(2)同時添加一個以test開頭的方法,并調用測試方法;這樣做主要是因為執行測試時JUnit(除本身特有的方法出外)只執行以test為開頭的方法;
(3)同時大家注意到我還把測試初始數據放到兩個private方法中private void setOrgTypeVOAddInfo()和private void setOrgTypeVOUpdateInfo()方法中供調用;