JUnit in java 真正的测试用例实战

发表于:2014-11-13 11:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:17173java    来源:51Testing软件测试网采编

  选择你要测试类中的方法,点击完成!便生成测试类的基本框架,如下代码,我们以对一个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()方法清理对象;
32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号