选择你要测试类中的方法,点击完成!便生成测试类的基本框架,如下代码,我们以对一个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开头中的方法对数据添加断言方法,同时提供很多断言的方法,
当我们写完所有方法策略后,JUnit测试如下图:
在方法页面中点击右键在“调试方式”或“运行方式”中点击“JUnit 测试”,就运行测试类!
在执行测试类时,执行的大概过程:
(1)先执行构造方法public OrgTypeDAOTest(String arg0) ;
(2)再执行初始化数据方法protected void setUp() ;
(3)再执行以test开头的测试方法;
(4)最后执行protected void tearDown()方法清理对象;