51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4253|回复: 8

(转载--基于J2EE单元测试)--实例

[复制链接]

该用户从未签到

发表于 2006-3-6 13:48:08 | 显示全部楼层 |阅读模式
import junit.framework.*;

import java.util.Vector;

public class VectorTest extends TestCase {

protected Vector fEmpty;

protected Vector fFull;

public VectorTest(String name) {

super(name);

}

public static void main (String[] args) {

junit.textui.TestRunner.run (suite());

}

protected void setUp() {

fEmpty= new Vector();

fFull= new Vector();

fFull.addElement(new Integer(1));

fFull.addElement(new Integer(2));

fFull.addElement(new Integer(3));

}

public static Test suite() {

return new TestSuite(VectorTest.class);

}

public void testCapacity() {

int size= fFull.size();

for (int i= 0; i < 100; i++)

fFull.addElement(new Integer(i));

assertTrue(fFull.size() == 100+size);

}

public void testClone() {

Vector clone= (Vector)fFull.clone();

assertTrue(clone.size() == fFull.size());

assertTrue(clone.contains(new Integer(1)));

}

public void testContains() {

assertTrue(fFull.contains(new Integer(1)));

assertTrue(!fEmpty.contains(new Integer(1)));

}

public void testElementAt() {

Integer i= (Integer)fFull.elementAt(0);

assertTrue(i.intValue() == 1);

try {

Integer j= (Integer)fFull.elementAt(fFull.size());

} catch (ArrayIndexOutOfBoundsException e) {

return;

}

fail("Should raise an ArrayIndexOutOfBoundsException");

}

public void testRemoveAll() {

fFull.removeAllElements();

fEmpty.removeAllElements();

assertTrue(fFull.isEmpty());

assertTrue(fEmpty.isEmpty());

}

public void testRemoveElement() {

fFull.removeElement(new Integer(3));

assertTrue(!fFull.contains(new Integer(3)) );

}

}

import junit.framework.*;

import junit.runner.BaseTestRunner;

public class AllTests {

public static void main(String[] args) {

junit.textui.TestRunner.run(suite());

}

public static Test suite() {

TestSuite suite= new TestSuite("Framework Tests");

suite.addTestSuite(ExtensionTest.class);

suite.addTestSuite(TestCaseTest.class);

suite.addTest(SuiteTest.suite()); suite.addTestSuite(ExceptionTestCaseTest.class);

suite.addTestSuite(TestListenerTest.class);

suite.addTestSuite(ActiveTestTest.class);

suite.addTestSuite(AssertTest.class);

suite.addTestSuite(StackFilterTest.class);

suite.addTestSuite(SorterTest.class);

suite.addTestSuite(RepeatedTestTest.class);

suite.addTestSuite(TestImplementorTest.class);

if (!BaseTestRunner.inVAJava()) {

suite.addTestSuite(TextRunnerTest.class);

if (!isJDK11())

suite.addTest(new TestSuite(TestCaseClassLoaderTest.class));

}

return suite;

}

static boolean isJDK11() {

String version= System.getProperty("java.version");

return version.startsWith("1.1");

}

}
回复

使用道具 举报

该用户从未签到

 楼主| 发表于 2006-3-6 13:48:43 | 显示全部楼层

这段我 还没理解清楚?
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2006-5-10 00:33:55 | 显示全部楼层
我门新手也看不懂,最好能说明以下他的功能
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2006-7-10 14:09:18 | 显示全部楼层
高手,还是给点注释吧
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2006-8-13 17:43:09 | 显示全部楼层

注释啊,注释啊

是啊,大哥,给点注释吧
俺笨啊
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2006-8-21 17:34:48 | 显示全部楼层
这个不如改一改名字,叫做使用JUNIT做JAVA程序的单元测试。和J2EE没啥关系。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2006-8-21 17:39:40 | 显示全部楼层
大概解释下吧我~~

JUNIT是一个开源的JAVA单元测试工具,或者叫解决方案也好

然后JUNIT里面提供了一个FRAMEWORK,其中的TEST SUITE可以理解成为一个SCENARIO,其中可以包括N个TEST CASE,每个TEST CASE可以包括N个TEST,是这样的一个层次结构。

然后这个例子是演示了如果用JUNIT做JAVA.UTIL.VECTOR这样一个数据类型的单元测试,其中测试了一些诸如添加,删除,克隆数据元素的操作。

junit.textui.TestRunner.run(suite())这个就是JUNIT最后运行的方式,除了这个TEXTUI外,我记得还有两个UI,分别是AWT和SWING的。

这个例子大概就是如此~~
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2007-9-5 11:21:39 | 显示全部楼层
看不明白!!
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2007-9-10 15:32:57 | 显示全部楼层
单元测试---Junit。
   对于一个类,方法的测试,是通过编写TestCase实例实现的。而这个测试的类必须继承TestCase类。
TestSuit是多个TestCase的组合,可以把多个类中的方法放在一起测,也可以把一个类中的方法分开测。单元测试的执行方法有Swing、文本方式等。对于开发人员来讲可以先设计好TestCase后,再去写类,这样考虑比较全面,不容易出错。
回复 支持 反对

使用道具 举报

本版积分规则

关闭

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

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

GMT+8, 2024-3-29 17:58 , Processed in 0.075014 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

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